Jump to content

On Click Sprite Not Working (array)


jjwallace
 Share

Recommended Posts

for (var i = 0; i < 40; i++){
            if(Math.floor((Math.random() * 4) + 1) == 3){
                fishyitem = 0;
                fishy = this.add.sprite(this.world.randomX, this.world.randomY, 'egg1');
                fishy.scale.set(0.2 + (0.03 * Math.floor((Math.random() * 10) + 1)));
                fishy.angle = -180 + Math.floor((Math.random() * 360) + 1);
                fishySpeed = 0;
                fishy.inputEnabled = true;
                fishy.input.useHandCursor = true; //if you want a hand cursor
                fishy.events.onInputOver.add(listenerClick, this); //<------------------------------------------------------

 

 

and the button

listenerClick: function (sprite, pointer) {
        fishyitem = 10;
        fishy.loadTexture = 'fish1';
    },

Link to comment
Share on other sites

fishy is out of scope when your event listener fires. However, you'll notice that you are passed an argument called sprite in that event listener. Try using that instead.

listenerClick: function (sprite, pointer) {
        sprite.fishyitem = 10;
        sprite.loadTexture('fish1');
    },

loadTexture is a function. I've fixed that for you in the code snippet I've provided.

It also looks like you just want fishyitem and fishSpeed to be properties of each fishy. You'll need to hold a reference to each variable in each fishy. Something like this:

fishy.fishyitem = 0;
fishy.fishySpeed = 0;

If you decide to go that route, the event listener I provided takes that into account as well.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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