dr.au Posted January 3, 2016 Share Posted January 3, 2016 (edited) How should my coinPickup() function get called?I've tried this two ways:1. Set the game.pickingupCoin=true flag during tween.onComplete, but it's too slow waiting on each coin tween to end2. Or set it during tween.onStart it's too fast and allows multiple coin values before a single coin is killed What do you guys recommend?var game = {}; // global for easy variable trackingfunction create() { // create coins for (var i = 0; i < vars.coinTotal; i++) { var coin = game.coins.create(x, y, 'coin'); coin.tween = game.add.tween(coin).to({ alpha: 0, y: 80 }, 1000); coin.tween.onComplete.add(function(coin, tween) { coin.kill(); game.pickingupCoin=false; // long coin pickup animations block other coins from being picked up, is there a better way to handle this? }); }}function update() { // add pickup event game.physics.arcade.overlap(player, coin, coinPickup, null, this);}function coinPickup(player,coin) { if (!game.pickingupCoin) { game.pickingupCoin=true; // prevent function from being called more than once per coin pickup game.coinCount += 1; // update count game.coinText.text = game.coinCount; // update UI coin.tween.start(); // animate }} EDIT: Here's what I ended up with after dropping my global game.pickingupCoin flag:function coinPickup(player,coin) { if (!coin.pickedup) { // check for individual coin flag coin.tween.start(); coin.pickedup = true; // set individual coin flag to true, and prevent this coin from being picked up more than once }} Edited January 3, 2016 by dr.au Link to comment Share on other sites More sharing options...
MichaelD Posted January 3, 2016 Share Posted January 3, 2016 1) 1000ms = 1second which is kinda a lot of time for a tween, have you tried 400ms or something similar2) What is wrong if multiple coins get picked? You can remove the game.pickingupCoin check and let the collision dictate when a coin is picked up.3) Try to add some space (gap) between the coins in order to allow the animation to take place.4) Instead of tween try to have animation frames for the coins and just play that animation, sometimes it is cheaper than tweens (in performance terms) Link to comment Share on other sites More sharing options...
chg Posted January 3, 2016 Share Posted January 3, 2016 Unless the player should only ever be allowed to be collecting a single coin at a time, I don't think your pickingupCoin flag on the game object is a good idea. If the intention is to stop the same coin from being collected multiple times then this state should be a property of the individual coin objects rather then a shared property common to all the coins collectively - this needn't be a flag (though it could be), for instance you could remove the coin object from your coins group (so the game stops wasting time testing it for further collisions) Regarding the function, I think it should be called in your collision test as per your code - it would seem backwards to me to call the start function on the tween and have that have a listener that calls coinPickup(). Link to comment Share on other sites More sharing options...
dr.au Posted January 3, 2016 Author Share Posted January 3, 2016 1) 1000ms = 1second which is kinda a lot of time for a tween, have you tried 400ms or something similar2) What is wrong if multiple coins get picked? You can remove the game.pickingupCoin check and let the collision dictate when a coin is picked up.3) Try to add some space (gap) between the coins in order to allow the animation to take place.4) Instead of tween try to have animation frames for the coins and just play that animation, sometimes it is cheaper than tweens (in performance terms) Thanks MichaelD, Yeah I played with different timers but ultimately I wanted to use the flag to tighten the logic. Unless the player should only ever be allowed to be collecting a single coin at a time, I don't think your pickingupCoin flag on the game object is a good idea. If the intention is to stop the same coin from being collected multiple times then this state should be a property of the individual coin objects rather then a shared property common to all the coins collectively - this needn't be a flag (though it could be), for instance you could remove the coin object from your coins group (so the game stops wasting time testing it for further collisions)Regarding the function, I think it should be called in your collision test as per your code - it would seem backwards to me to call the start function on the tween and have that have a listener that calls coinPickup(). @chg Yes! Thanks so much for explaining it like this. I had the idea right for the flag but I was attaching it to the global scope instead of per coin, what a great idea! Just implemented that and it's working great now! Thanks guys. Link to comment Share on other sites More sharing options...
Recommended Posts