Creating multiple sprites of the same name

Hello All,
This post is similar to a previous, but slightly different. I am trying to make a version of Snake. I have a system for moving and turning on a 6x6 ‘grid’, but what I want to do is create body parts behind the head as the snake moves about and then have the lifespan, delete the body part as it expires…creating the illusion of a moving body. Easy to do in other engines, but trying to figure out arcade.

This is what I was hoping would work, but it is failing to create additional myXBodyParts on each loop. And the lifespan doesn’t work either. I know I could create an array to create multiple instances of myXBodyParts, but seems like over kill.

It sure would be nice if there was more of an object concept that could have multiple instances using a sprite. Is creating an array really the only way in arcade?

BTW: I tried making these additional parts as projectiles, which allowed me to create multiple instances AND the lifespan worked, BUT, even after I set the projectile VX and VY to zero, they still moved.

Couple different things! It looks like you’re not setting the x and y of the body part after you create it, so it’s creating multiple body parts all in the exact same location. Try adding this to your code:

Second, the projectile code is really just the “create sprite” code with some extra flags and initial values set. The problem you’re running into is that we try to infer an initial “direction” for the projectile based on the vx and vy in the block, and when those are both set to zero we pick a random direction–I’ve filed a bug for this, since it does seem like unexpected behavior.

Thanks! I think the x y setting was okay as when I used projectiles, they were located correctly, but flying all over the place. I went ahead and wrote the code using an array of sprites. It would be nice to zero the velocity of projectiles.

That said, my version of snake is looking good. About ready to start recording the instructional videos. I wish arcade was a bit more advanced with multiple sprites of the same type/name. BUT, I discovered a cool way to get what i wanted:

I created an index variable (varBodyIndex) for my array of sprites and used it when creating the sprite.

I forgot to increment my index, but my code is working great because I have no need to ever reference the array again. So this is a cool trick to create multiple/independent sprites of the same type! You can just use list position 0 for all of them! For this game at least, I just needed them to self-destruct…no array management needed!

1 Like