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 =;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?

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.