Jump to content

Detecting click on child in a group of sprites


umair-khan
 Share

Recommended Posts

So I have a group, and it has three sprites.

 

create() {
    
        var parent = game.add.group();

        var allSprites = [ 'sprite1', 'sprite2', 'sprite3'];

        for (i=0; i<3; i++){
          childImage = parent.create( game.world.centerX,  game.world.centerY+ (i*30), allSprites);
          childImage.anchor.setTo(0.5, 1);
          childImage.inputEnabled = true;
          childImage.input.useHandCursor = true;
        }

}

 

 

How to I trigger function changeText(); when one of the child sprite is clicked.

So far I have tried this method below but  it only detects click on the last sprite.

update(){
    child.events.onInputDown.add(changeText, this);
}

Link to comment
Share on other sites

for (var i = 0; i < 3; i++) {

childImage = parent.create( game.world.centerX, game.world.centerY+ (i*30), allSprites[i]);

childImage.anchor.setTo(0.5, 1);

childImage.inputEnabled = true;

childImage.input.useHandCursor = true;

childImage.events.onInputDown.add(yourClickFunction, this);

}

...

function yourClickFunction(sprite, pointer) {

sprite.alpha = 0.5;

}

Link to comment
Share on other sites

  • 1 year later...

Sorry for raising a dead horse from the grave here, but i'm having major troubles with this solution when using a gamestates.

 


 

function yourClickFunction(sprite, pointer) {

sprite.alpha = 0.5;

}

i would have to use this. here (this.sprite, this.pointer) . But that throws up an error.

Thank you for your time.

 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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