Stephen304

Can I ID sprites of a group to be referenced?

Recommended Posts

I have a group of sprites and I am trying to find a way to ID them to be referenced (to change velocity for instance). I can't just make individual sprites because that would hard code the number of sprites which needs to be dynamic.

 

Is there a way of doing something like:

snakes = game.add.group();snakes.create(400, 400, 'snake1', 'snakeImage');snakes.create(400, 400, 'snake2', 'snakeImage');snakes.create(400, 400, 'snake3', 'snakeImage');snakes.create(400, 400, 'snake4', 'snakeImage');

Then:

snakes['snake3'].body.velocity.x = 150;

Or something of that effect? I am considering making an object like this:

var snakes = {};snakes['andrew'] = game.add.sprite(400, 400, 'snakeImage');snakes['lucy'] = game.add.sprite(400, 400, 'snakeImage');snakes['bob'] = game.add.sprite(400, 400, 'snakeImage');snakes['batman'] = game.add.sprite(400, 400, 'snakeImage');

And:

snakes['batman'].body.velocity.y = -9000;

Is there a better way to do this?

Share this post


Link to post
Share on other sites

Sprites have a 'name' property which you could use for this, but there's no way to select a sprite from a Group based on the name (they aren't stored like that anyway, so even if I added that function it wouldn't be very fast). You could get the index ID of the child in the group and store that locally, but if the group is constantly changing again this won't be that reliable right now.

 

In short I think your current approach is probably best.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.