Jump to content

Cancel a TimerEvent to stop idle animation?


Recommended Posts

Hi all, what I'm trying to do is the following:
When the player character is idle it should do an idle animation every now and then. So like blinking his eyes or yawning or something like that.
To do this I've added the idle animations and then set a TimerEvent to wait for a random period. The TimerEvent triggers a function that plays an animation and then sets the TimerEvent again for another random period. However, when the player does an action it will start another animation, like walking or jumping, so the TimerEvent that will trigger the idle animation should be canceled.
Problem is: how do I cancel a TimerEvent ? I couldn't find anything in the docs.
Or failing that: what's another good way to trigger/cancel an idle animation?
Here is my example code:
    create: function() {               // add sprite        this.guy  = this.game.add.sprite(100, 100, 'blocks60x60', 80);        this.guy.animations.add ('blink', [10, 11, 12, 11, 10], 15, false); // frameRate=15ms, loop=false        this.guy.animations.add ('yawn', [13, 13, 14, 14, 15, 16, 15, 16, 15, 14, 13], 15, false); // frameRate=15ms, loop=false        // initialise blink timeout        this.onBlinkTimeout();                // click anywhere to cancel animation        this.game.input.onDown.add(this.onTapGame, this);    },    onBlinkTimeout: function() {        this.guy.animations.play('blink');        // set timeout again for next blink        var ms = 500 + this.game.rnd.integerInRange(0, 1000); // random 500..1500 ms        this.blinktimeevent  = this.game.time.events.add(ms, this.onBlinkTimeout, this);            },    onTapGame: function() {        debugger;        this.blinktimeevent.cancel(); // ?? how to cancel?    }


Link to comment
Share on other sites


  • Recently Browsing   0 members

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