umair-khan

Detecting click on child in a group of sprites

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);
}

Share this post


Link to post
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;

}

Share this post


Link to post
Share on other sites

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.

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.