ylluminarious Posted April 23, 2015 Share Posted April 23, 2015 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 More sharing options...
lukaMis Posted April 23, 2015 Share Posted April 23, 2015 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. ylluminarious 1 Link to comment Share on other sites More sharing options...
ylluminarious Posted April 24, 2015 Author Share Posted April 24, 2015 Thanks! This looks like it will work, but I'll have to test it out to be sure. Link to comment Share on other sites More sharing options...
rsadwick Posted April 24, 2015 Share Posted April 24, 2015 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. scofield and ylluminarious 2 Link to comment Share on other sites More sharing options...
ylluminarious Posted April 24, 2015 Author Share Posted April 24, 2015 @rsadwick That is also an interesting idea. I might look into that too. Link to comment Share on other sites More sharing options...
rsadwick Posted April 24, 2015 Share Posted April 24, 2015 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. ylluminarious, scofield, Carlos and 6 others 9 Link to comment Share on other sites More sharing options...
ZoomBox Posted April 24, 2015 Share Posted April 24, 2015 You should definitly work with events in those cases. Link to comment Share on other sites More sharing options...
lukaMis Posted April 24, 2015 Share Posted April 24, 2015 @rsadwick This looks like proper way to do it, unlike my suggestion that is more like hack & slash type of suggestion. I will start using these internal events too. Link to comment Share on other sites More sharing options...
dr.au Posted January 2, 2016 Share Posted January 2, 2016 (edited) 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 January 4, 2016 by dr.au Link to comment Share on other sites More sharing options...
Recommended Posts