Stephen304 Posted January 21, 2014 Share Posted January 21, 2014 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? shohan4556 1 Link to comment Share on other sites More sharing options...
rich Posted January 22, 2014 Share Posted January 22, 2014 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 More sharing options...
Stephen304 Posted January 22, 2014 Author Share Posted January 22, 2014 Alright thanks! Link to comment Share on other sites More sharing options...
Recommended Posts