2017년 5월 19일 금요일

Is making multiple instances of a sprite possible?


I found one topic that said no so I want to confirm this as this is a fairly basic thing you should be able to do and I really hope I don't have to learn how to use android studio although I do know java. I'm making a game where if you collide with a certain object, a duplicate of that object will appear elsewhere on the screen. Is there any way to duplicate them? or do you actually have to make a whole bunch of invisible/inactive sprites on the canvas to have this same effect? 

--
You cannot instantiate Sprites at runtime, so as you mentioned, you will have to have a bunch of them hidden if you want to do something like that.

--
I'm making a game where if you collide with a certain object, a duplicate of that object will appear elsewhere on the screen.
If your first instance disappears when the copy appears it is a simple process of moving the original sprite to the new location.
If the sprite disappears during the move and there is a delay between first position and second postion, then use a clock timer and the ImageSprite Visible property to hide then reveal the sprite in the new location.

--
So a snake style game would be a hassle to make then? I would have to make up to a 100 invisible sprites to appear, and then  in the code have a counter to calling the next sprite in the list?

--
You might be able to do a snake game with the Canvas DrawLine event rather than sprites.

--
I would have to make up to a 100 invisible sprites to appear, and then  in the code have a counter to calling the next sprite in the list?
Yes. That is the way I would do it.
Keep the component blocks for the ImageSprites in a list from 1 to the number of segments in the snake.

item 1 is the tip of the tail, item n is the head.

Then treat the list as a FIFO queue.

For the snake to move,
 the tip of the tail becomes the new head.
The the old head becomes a body segment.
The second segment becomes the new tail.

The list of components is initially:
item 1 is the tip of the tail
item 2 is the segment after the tip of the tail
(these middle items are body segments)
item n-1 is body segment after the head
item n is the head.

Now, for the snake to move forward one segment,

the list is appended with the value from item 1 to become the new head.
Item 1 is removed.
the new item 1 becomes the tip of the tail.
item n-1 (the old head) becomes a body segment.

Once the component blocks have been set in the list, their respective Images can be set to body segment to tail tip, old head to body segment, tail tip to head.
And the snake redrawn perhaps with a new angle for the head (make sure Rotates is enabled for all ImageSprites)

Each time the snake moves a segment, the process is repeated.

There may be a sinpler way to do it, but this seems a logical way to me.

--

댓글 없음:

댓글 쓰기