ylluminarious

Call a function in the update method only once

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?

Share this post


Link to post
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. 

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.