Jump to content

Call a function in the update method only once


ylluminarious
 Share

Recommended Posts

Hi all,

 

I'm trying to attempt the following sort of thing in my game in its update method:

var update = function () {  // other update method stuff up here    if (conditionIsMet) {    someFunction(); // only call this function 1 time  }};

The problem with the above code is that `someFunction` will get called on every iteration of the update method that the condition runs as true. I want the condition to be continually checked, but I do not want `someFunction` to get called over and over again with the update method, only one time. Is this possible to do? If so, how?

Link to comment
Share on other sites

here is one way:

var counter = 0;var update = function () {// other update method stuff up hereif (conditionIsMet && counter === 0) { someFunction(); // only call this function 1 time}};function someFunction () { ...your code....counter++;}

someFunction will be called only once as additional condition is that counter is 0. Than you update that counter in someFunction hence someFunction will not be called again. If needed you can reset counter (if some other condition is met) and you again have someFunction called only once. 

Link to comment
Share on other sites

Remember that your game will check that every update.  You should look into Signals, Phaser's events.  

 

You can listen for an event, for example "OnPlayerDamage".  Within some condition, you can dispatch the event.  Once the event happens, the listener will pick it up and then you can do something, like subtract lives from the player.

Link to comment
Share on other sites

Here is a small example of how Signals work:

//define the signal:game.events.onPlayerDamage = new Phaser.Signal();//The listener:game.events.onPlayerDamage.add(SomeFunctionToCallWhenEventDispatches, this);//Dispatch:game.events.onPlayerDamage.dispatch();

I try to keep my Update function as small as possible to use Signals where needed.

Link to comment
Share on other sites

  • 8 months later...

I ended up doing something similar with my coinPickup() function:

function create() {  // create coins  for (var i = 0; i < vars.coinTotal; i++) {    var coin = game.coins.create(x, 0, 'coin');        coin.tween = game.add.tween(coin).to({ alpha: 0, y: 80, x: coin.x+(game.width/1.8) }, 1000, Phaser.Easing.Cubic.Out);    coin.pickedUp = false; // set flag on each coin to prevent multiple update calls    coin.tween.onComplete.add(function(coin, tween) {      coin.kill();    });  }}function update() {  // add collide event for pickup  game.physics.arcade.overlap(player, coin, coinPickup, null, this);}function coinPickup(player,coin) {  if (!coin.pickedUp) { // check if coin has already been picked up, if not proceed...    coin.pickedUp=true; // then immediately set it to true so it is only called once    game.coinCount += 1;    coin.tween.start();  }}
Edited by dr.au
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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