简体   繁体   中英

Timer in javascript phaser game

I am trying to create a timer to limit the amount of time a player can use their jetpack in my phaser game. I've been following this( http://www.joshmorony.com/how-to-create-an-accurate-timer-for-phaser-games/ ) but I can't get anything to work. Is there a better way to implement this?

var use_jetpack;  

jetpack_timer = game.time.events.add(
    Phaser.Timer.SECOND * 1 /*change the 1 to however many seconds you want*/,
    end_jetpack /*runs the function end_jetpack*/,
    this);

function end_jetpack(){
    use_jetpack = false;
    //if you want a cool down, put it here. follow what was done in line 3.
}

if(use_jetpack){
   //player can use jetpack
}else{
   //player cannot use jetpack
}

Try this. I don't know what you couldn't get to work, but this is what my implementation would be. Of course you would need to put functions in update(), and the variable wherever you put those. This is just a quick demo. Also I had odd formatting for teaching purposes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM