Nick Posted February 1, 2014 Share Posted February 1, 2014 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 More sharing options...
XekeDeath Posted February 1, 2014 Share Posted February 1, 2014 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 itemsprite.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 More sharing options...
Nick Posted February 1, 2014 Author Share Posted February 1, 2014 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 More sharing options...
jcs Posted February 1, 2014 Share Posted February 1, 2014 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" ]; plicatibu, jerome and Nick 3 Link to comment Share on other sites More sharing options...
Nick Posted February 2, 2014 Author Share Posted February 2, 2014 Thanks JCS. I've used your addSprite method and it's working well. I'm also cleaning up the map by removing any sprites that are no longer 'alive' once a state changes. Link to comment Share on other sites More sharing options...
jcs Posted February 3, 2014 Share Posted February 3, 2014 glad that helped Link to comment Share on other sites More sharing options...
Recommended Posts