Jump to content

How to call a function every once a second, instead of every frame with isDown.


Recommended Posts

So I'm learning Phaser and Javascript by creating a simple space figher game.

You fly around from left to right and shoot the enemy's that are attacking jou from above.

the player shoots bullets with the UP key. When the up key is pressed, the function shoot() is called.

Obviously, because the isDown check is performed in the update(), it will trigger 60 times a second as long as the up key is pressed.


Can i perform some action to let it once get checked once a second, or atleast something that the function only can be called again after a second has passed.


My update()

function update(){    cursors = game.input.keyboard.createCursorKeys()	    // Checks if UP is pressed on the keyboard    if(cursors.up.isDown)    {	shoot();    }}

Shoot function:

function shoot(){    bullet = game.add.sprite(playerShip.x + 16, playerShip.y, 'bullet');    playerShip.bringToTop();    game.physics.arcade.enable(bullet);    bullet.body.velocity.y = -500;}

Thanks in advance :)

Link to comment
Share on other sites


  • Recently Browsing   0 members

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