ethan.sletteland Posted October 5, 2015 Share Posted October 5, 2015 Hey all,Sorry if this is addressed elsewhere, but I RTFM'ed and I can't find it. What is best practice for getting a handle for a sprite on the stage? I'm working on a sidescroller, using tiled and the import and loop technique described in this tutorial:http://gamedevacademy.org/html5-phaser-tutorial-top-down-games-with-tiledAt some point I want my player to collide with one sprite (a switch), and then I want to change the state of another sprite (a door). My thought is to do this by creating a global dictionary object and registering each sprite with it as I create them, and I can certainly do that, but I feel like I might be recreating a wheel here. Is there some kind of getSpriteById() that I'm too dumb to see? Link to comment Share on other sites More sharing options...
in mono Posted October 6, 2015 Share Posted October 6, 2015 The way I'd do it:Extend Phaser.Sprite to create the switch; inside the Switch class (or the name you choose) make a reference to the door and assign it when creating that switch. Then you'll be able to access that door using that reference. Link to comment Share on other sites More sharing options...
MichaelD Posted October 6, 2015 Share Posted October 6, 2015 you can create a global object like this: var registry = { door:{}};// save reference like thisregistry.door = game.add.sprite(0,0,"door");You can totally do that, but carefull not to overdo it, because it will be difficult to manage. Also in monos' answer is correct. However there is no fast way to search by ID, although you can reference by index, if your door is inside a group and its the 1st child you can do var door = myObjects.getChildAt(0);Or if you set that your first door to encounter is the only one with the "alive = true" property set, you can do:var door = myObjects.getFirstAlive(); Link to comment Share on other sites More sharing options...
drhayes Posted October 6, 2015 Share Posted October 6, 2015 In a game I'm working on the piece of code that loads the map tracks any created entity with a name in a special object. Later, when configuring the switches, a switch that has a target with the same name gets a reference to the entity. A lot like what MichaelD said. MichaelD 1 Link to comment Share on other sites More sharing options...
ethan.sletteland Posted October 6, 2015 Author Share Posted October 6, 2015 you can create a global object like this: var registry = { door:{}};// save reference like thisregistry.door = game.add.sprite(0,0,"door");You can totally do that, but carefull not to overdo it, because it will be difficult to manage. Also in monos' answer is correct. However there is no fast way to search by ID, although you can reference by index, if your door is inside a group and its the 1st child you can do var door = myObjects.getChildAt(0);Or if you set that your first door to encounter is the only one with the "alive = true" property set, you can do:var door = myObjects.getFirstAlive();What I ended up using was similar, but a little more dynamic.if(element.properties.hasOwnProperty('trigger')){ objects.trigger[element.properties.trigger] = sprite;}where element is the loop entry, sprite is the handle for the newly created sprite, and trigger is arbitrary attribute I added in tiled. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts