JavaNerd Posted March 4, 2018 Share Posted March 4, 2018 When the user clicks on the screen, it spawns in a sprite. I'm trying to reference these sprites later, how can I do that? For example: Sprite1.name = "Example"; Sprite1.destroy(); Sprite2.name = "Example"; Sprite2.destroy(); Link to comment Share on other sites More sharing options...
PsichiX Posted March 4, 2018 Share Posted March 4, 2018 if Sprite1 is reference to dynamicaly created sprite, then you can pass it to whatever place you like. your explanation of problem is weak, so is answer, please tell us what goal you want to achieve, not what you've tried to code. Link to comment Share on other sites More sharing options...
JavaNerd Posted March 4, 2018 Author Share Posted March 4, 2018 Sorry, last time I coded is when I was working with AS2, and I'm picking this up slowly. var sprite = this.add.sprite(pointer.x + cam.scrollX, pointer.y + cam.scrollY, 'small_fence').setInteractive(); sprite.iName = fenceCount; sprite.on('pointerdown', function () { this.destroy(); }) I spawn sprites in and make them interactive. What I'm trying to do is, I want to reference these sprites later. How can I reference a specific sprite? For example in AS2, I used to be able to give them an instance name like "Sprite1." I would then be able to reference it later like: _root.Sprite1 Is there a way to do this with Phaser, so I can manipulate the sprite when I can't use this. Link to comment Share on other sites More sharing options...
PsichiX Posted March 4, 2018 Share Posted March 4, 2018 you still did not told us what this reference will be used for, so general answer is: create some global container: game._globals = new Map(); then add sprite to it: game._globals.set('Sprite1', sprite); and use it later: game._globals.get('Sprite1').destroy(); and remove; game._globals.delete('Sprite1'); Link to comment Share on other sites More sharing options...
JavaNerd Posted March 4, 2018 Author Share Posted March 4, 2018 This example worked. Thank you. Link to comment Share on other sites More sharing options...
Recommended Posts