Jump to content

Call a function inside events with states


deepsky88
 Share

Recommended Posts

hi, i've a problem in my code, chrome said:"Cannot read property 'apply' of undefined", below the code interested in:

(all after the end of update function)

if (game.rnd.integerInRange(0, 50) == 1) {
                    nuvoletta.visible = true;
 
                    game.time.events.add(Phaser.Timer.SECOND / 1.55, this.riflessi, this);
},
riflessi: function () {
            if ((nuvoletta.visible) && (!pulsanteblocco.downDuration(1000))) {
                pulsanteblocco.isDown = false;
                nuvoletta.visible = false;
                if (life5.visible) {
                    life5.visible = false;
                    (game.add.audio('danno1')).play();
                }
                else if (life4.visible) {
                    life4.visible = false;
                    (game.add.audio('danno1')).play();
                }
                else if (life3.visible) {
                    life3.visible = false;
                    (game.add.audio('danno1')).play();
                }
                else if (life2.visible) {
                    life2.visible = false;
                    (game.add.audio('danno1')).play();
                }
                else if (life1.visible) {
                    life1.visible = false;
                    player.frame = 23;
                    player.body.velocity = 0;
                }
            }
            else if ((!stamina1.visible) && (nuvoletta.visible)) {
                stamina1.visible = true;
            }
            else if ((!stamina2.visible) && (nuvoletta.visible)) {
                stamina2.visible = true;
            }
            else if ((!stamina3.visible) && (nuvoletta.visible)) {
                stamina3.visible = true;
            }
 
            if ((nuvoletta.visible) && (pulsanteblocco.downDuration(1000))) { (game.add.audio('blocco1')).play(); }
            nuvoletta.visible = false;
        },
Link to comment
Share on other sites

I had to call an anonymous function inside the event timer, haven't found any other solution...

Now i'm stuck with this little guy:

game.physics.arcade.overlap(player, exp, function (player,Exp) {

                Exp.kill();

                this.esperienza += 10;

                exptesto.text = 'Esperienza: ' + this.esperienza;

                music6.play()

            },null, this),

 

the score(exptesto in my example) report always NaN, any suggestion?

Link to comment
Share on other sites

@deepsky88 Hard to say from just that snippet, but I'm guessing "this.esperienza" doesn't exist on 'this', so performing a += 10 operation returns the NotANumber (NaN) property because it's operating on a property that can't be accessed.

First thing: has this.esperienza actually been created, and what is it: just a basic type like a Number, Sprite etc?

Second, it's always worth debugging yourself to see why a variable or object isn't behaving as expected: either adding the debugger breakpoint (perhaps after this.esperienza += 10 assignment) to see what the scope of your anonymous function contains, though you can do the same with just a console.log(this) in the same place (though sometimes with Phaser that can cause a cascade of console.logs!)

Link to comment
Share on other sites

i create "var esperienza = 0" in the boot, like all other variables and assets, that little snippet is all in update function, i have to try to separate the callback into a separate function outside the update, the main problem is that without this, esperienza is not defined, thx for answer and inputs, i'll try some things and let u know =)

Link to comment
Share on other sites

4 minutes ago, deepsky88 said:

i create "var esperienza = 0" in the boot, like all other variables and assets, that little snippet is all in update function, i have to try to separate the callback into a separate function outside the update, the main problem is that without this, esperienza is not defined, thx for answer and inputs, i'll try some things and let u know =)

@deepsky88 There is your problem:

var esperienza = 0

this is a local variable, not a member property of your Game State; it's scope is only wherever it was declared - the boot function. So assuming you mean the preload() or create() functions, if you declare it like the below instead, it SHOULD be visible to the physics callback:

this.esperienza = 0;

 

Link to comment
Share on other sites

8 hours ago, pixelburp said:

@deepsky88 There is your problem:


var esperienza = 0

this is a local variable, not a member property of your Game State; it's scope is only wherever it was declared - the boot function. So assuming you mean the preload() or create() functions, if you declare it like the below instead, it SHOULD be visible to the physics callback:


this.esperienza = 0;

 

not working, always NaN; worked a bit if a define var esperienza in the update function, but after one second returns 0, i'm trying to callback the function outside the update right now

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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