Jump to content

Find sprites by a unique indentifier


Recommended Posts

I'm creating a tutorial for my game and I need to be able to create arrows which point out particular elements on the screen.  I want to be able to give my sprites a unique identifier such as 'hud' and then while my tutorial is playing I can find the 'hud' sprite and position the arrow next to it.


Is there a way to assign a unique identifier to a sprite and then search the entire game ( e.g. a global collection of sprites ) to find it? 

Link to comment
Share on other sites

You could keep a reference to the items that need pointing to in a separate array, and then just point at the first one when needed, then the second one, etc.


As this is javascript, you can always just add a new property on an item

sprite.myNewProp = idNumber++;

If you still wanted to loop through everything (which I believe you can use game.world for), you can check for the existence of myNewProp, and check if it matches what you want to point to.

Link to comment
Share on other sites

Thanks for your help. Yeah I think I'm going to have to keep a reference of the items like you've suggested. This should work for my needs as there is a finite number of items that will need to be pointed too.  


It realised this may also be possible by looping through the children of each Pixi displayObjectContainer to find a sprite with a particular property, but that would result in a lot of recursion ( I have nested groups ). It would be nice if there were a way to simply 'pluck' an object with a given name from the game world. 

Link to comment
Share on other sites

if you want to look them up by name, keep a map of them yourself, similar to xekedeath's suggestion. it means a little extra housekeeping when adding/creating objects, but will pay off if you're having to look them up often.


to make it easier, use your own 'addXXX' function, which is pretty easy if you're only tracking one type of object (e.g. Sprites) and use unique IDs/names. e.g.

function addSprite( game, name, x, y, key, frame, group ) {  allObjectsMap[name] = game.add.sprite( x, y, key, frame, group );}var hud = allObjectsMap[ "hud" ];
Link to comment
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.
  • Create New...