Jump to content

Mouse click event not working for group


rahulng
 Share

Recommended Posts

Guys I have the following code to make a star sprite and 2 firstaidbox(added through group) sprites.what i want is that when i click the star the firstaid boxes should dim(alpha=0.5) and vice versa.however the mouse event doesnt seem to work when I click the firstaid boxes.

 

function create ()
        {
                    
                    starimage = game.add.sprite(100,100,"star");
            
                    starimage.inputEnabled=true;
                    starimage.events.onInputDown.add(listener,this)
                    
                    
                bots=game.add.group();//the firstaid boxes
                    for(var i=2;i<4;i++)
                {
                    var myvar=bots.create(i*100,100,'firstaid');
                    
                    myvar.inputEnabled=true;
                    
                    myvar.events.onInputDown.add(listener,this);
                }
                
        }
        
        function listener(sprite, pointer)
        {
            if(sprite==starimage)
            {
                sprite.alpha = 0.5;
                bots.alpha=1;
            }
            
            if(sprite==bots)
            {
                alert("clicked");
                sprite.alpha = 0.5;
                starimage.alpha=1;
            }
                    
        }

Link to comment
Share on other sites

Hmmm, I'm very new to both javascript and phaser, but if the == operator is anything like the == operator in java then your problem is in the

 

 if(sprite==bots) line.

 

bots is the group and sprite is just one of the sprites in that group so it will never evaluate to true even if the sprite is part of that group, assuming == works like it does in java. 

 

In my phaser code I've been looping through the group and checking each sprite against each entry in the group:

 

for (var i = 0; i < bots.length; i++) {

   if (bots.getAt(i) == sprite) {

      //bot onClick code here

   }

}

 

One of us is doing it the wrong way, and it could be me, but I thought I'd post just in case I am right because that would make sense and it'd be helpful if I was. When I'm at my computer and have more time I'm going to look into it because if your way is correct I could simplify my code a lot. if == can be implemented by each object similar to .equals in java then that'd explain it.

 

Hope I didn't waste your time, if I did then thanks though because that means you taught me something :)

Link to comment
Share on other sites

Moe is right,  if(sprite==bots) is your problem.

The callback is passed the sprite you clicked on, not the group it belongs to.

 

If that callback only belong to the star and first aid boxes, perhaps a simple else statement:

        function listener(sprite, pointer)        {            if(sprite==starimage)            {                sprite.alpha = 0.5;                bots.alpha=1; //<< Not sure about this working, you may have to call bots.setAll('alpha', 1);            }            else            {                alert("clicked");                sprite.alpha = 0.5;                starimage.alpha=1;            }        }
Link to comment
Share on other sites

Thanks a lot guys .both of you.you have to cycle through each bot to verify like moe91 said and XekeDeath you were right too thebots.alpha doesnt work.bots.setAll('alpha',1) works fine.just one question tho if i wanted to set only a few specific bots to alpha what would I do?

 
Link to comment
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...
 Share

  • Recently Browsing   0 members

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