James W Posted September 2, 2017 Share Posted September 2, 2017 Hi Guys, Complete phaser noob and JS amateur!! Probably an easy answer but how do I access a sprite having created it in a group? I'm not sure which sprite the user will choose first, second etc but need to be able to access the chosen sprites x,y coordinates as well as it's index in the group so I can disable input on that sprite. Hope this makes sense:) My code: //create sprite randNum = game.rnd.integerInRange(1,9); sheep = game.add.group(); game.physics.arcade.enable(sheep); sheep.enableBody = true; for (i = 0; i < randNum; i++){ randX = game.rnd.integerInRange(10,460); randY = game.rnd.integerInRange(150,540); var lamb = sheep.create(randX,randY,'sheep'); lamb.anchor.setTo(0.5,0.5); lamb.inputEnabled = true; lamb.input.enableDrag(true); lamb.events.onDragStart.add(dragStart); lamb.events.onDragStop.add(dragStop); } I would like to do something like this in the dragStop event: function dragStop(){ if (lamb.x > 500 && lamb.x < 750){ soundIndex++; lamb.inputEnabled = false; } Cheers J Link to comment Share on other sites More sharing options...
Taz Posted September 3, 2017 Share Posted September 3, 2017 You can see in this example for reference, https://phaser.io/examples/v2/input/drag-event-parameters, that the clicked sprite is a parameter to your dragStop function samme 1 Link to comment Share on other sites More sharing options...
samme Posted September 3, 2017 Share Posted September 3, 2017 For "random" access to group children, you can use Phaser.Group#getChildAt and Phaser.Group#getByName (or just save a reference from Phaser.Group#create). But for input handlers you'd want to use the sprite parameter in the callback. Taz 1 Link to comment Share on other sites More sharing options...
James W Posted September 4, 2017 Author Share Posted September 4, 2017 Perfect! Thanks Guys Link to comment Share on other sites More sharing options...
Recommended Posts