brentstrandy Posted May 18, 2016 Share Posted May 18, 2016 I'm having a hard time triggering onDestroy for my FreezeEffect object. I've essentially set up an object that destroys itself after a few seconds. I've tested and confirmed that "destroyObject" is being called. But sometimes the game ends, the state transitions in the middle of the FreezeEffect, and the "onDestroy" method is never called. I was under the assumption that calling state.start('newState') destroys the Phaser objects in the previous state. Is that not true? Example code: // Freeze Effect FreezeEffect = function (game) { ... constructor code ... ... create tween to fade freeze effect ... // Call function to destroy the freeze effect when the tween finishes tween.onComplete.add(this.destroyObject, this); // Slow down time game.time.slowMotion = 2.0; }; FreezeEffect.prototype = Object.create(Phaser.Group.prototype); FreezeEffect.prototype.constructor = FreezeEffect; FreezeEffect.prototype.destroyObject = function() { // Put time at normal speed this.game.time.slowMotion = 1.0; this.parentGroup.destroy(true, false); this.freezeSFX.destroy(); // Destroy the object and free memory this.destroy(true, false); }; FreezeEffect.prototype.onDestroy = function() { // Put time at normal speed this.game.time.slowMotion = 1.0; } Link to comment Share on other sites More sharing options...
Skeptron Posted May 18, 2016 Share Posted May 18, 2016 If onDestroy is an event, shouldn't you write this.onDestroy.add(function(){//whatever}); instead of making it a method? brentstrandy 1 Link to comment Share on other sites More sharing options...
brentstrandy Posted May 18, 2016 Author Share Posted May 18, 2016 Thank you @Skeptron - I had a syntactical lapse of judgement. Your solution works great! Link to comment Share on other sites More sharing options...
Recommended Posts