Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

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');


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');


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

Is there a better way to do this?

Link to comment
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.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...