Jump to content

How do I access an object/sprite in a group?


James W
 Share

Recommended Posts

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...