Zampano Posted November 10, 2017 Share Posted November 10, 2017 Hey guys, I've run into yet another case of "don't know how to do this best" and I'm looking for help Here's my Scenario: I've got a sprite with a certain tween. The tween is supposed to be skippable, which means it stops and all properties of the sprite will be set to the target values of the tween and the onComplete function will be fired. After that, it can be played again and so on. Here's the Tweens setup: this.portrait.inFocus = false; this.portrait.focusTween = this.add.tween(this.portrait).to({alpha: 1}, 1, Phaser.Easing.Linear.None, false, 0).to({alpha: 0}, 1000, Phaser.Easing.Quintic.Out, false, 0); this.portrait.inTween.onComplete.add(function(){this.inFocus = true}, this.portrait); Problem: Using stop() also marks the tween for deletion and it won't play again. My ideas: - Using stop() and adding a new tween each time. As I see it, the downside would be that I need to add the onComplete function every time, too. Also, I'm not so sure about the memory usage for this one. - Overriding the stop() function to not remove the tween. I tried it bycommenting out this.manager.remove(this), but it's already causing some weird behaviour with the tween jumping to it's start randomly after completing, and I'm a little bit afraid that could cause other damage elsewhere anyways.. So... I'm very open to suggestions and/or clarification Link to comment Share on other sites More sharing options...
samme Posted November 10, 2017 Share Posted November 10, 2017 I’ve used Phaser.Tween.prototype.reset = function() { var i, key, len, ref; this.isPaused = this.isRunning = false; this.timeline.length = 0; ref = Object.keys(this.properties); for (i = 0, len = ref.length; i < len; i++) { key = ref[i]; delete this[key]; } return this; }; Zampano 1 Link to comment Share on other sites More sharing options...
Tom Atom Posted November 11, 2017 Share Posted November 11, 2017 Hi, long time ago I solved reusing stopped tweens - here is detailed topic about it: Zampano and samme 2 Link to comment Share on other sites More sharing options...
Zampano Posted November 13, 2017 Author Share Posted November 13, 2017 Thank you both! Tom's approach was more like it since I want to use the exact tween again and not delete it's properties. I ended up doing it like this: Phaser.Tween.prototype.removeFromUpdateQueue = function () { var i = this.manager._tweens.indexOf(this); if (i !== -1) { this.manager._tweens.splice(i, 1); } else { i = this.manager._add.indexOf(this); if (i !== -1) { this.manager._add.splice(i, 1); } } } Phaser.Tween.prototype.skip = function () { if(this.isRunning || this.isPaused) { this.isRunning = this.isPaused = false; this.onComplete.dispatch(this.target, this); this._hasStarted = false; this.removeFromUpdateQueue(); var propnames = Object.keys(this.properties); for (var i = 0; i < propnames.length; i++) { this.target[propnames[i]] = this.properties[propnames[i]]; } } return this; } samme 1 Link to comment Share on other sites More sharing options...
samme Posted November 13, 2017 Share Posted November 13, 2017 Do you think Phaser should warn if you try to start a stopped tween (which will always fail)? That part has always seemed odd to me. Link to comment Share on other sites More sharing options...
samme Posted November 14, 2017 Share Posted November 14, 2017 phaser-ce/issues/401 Link to comment Share on other sites More sharing options...
Zampano Posted November 14, 2017 Author Share Posted November 14, 2017 I think that's a good idea. I had quite some trouble finding out that the "problem" is kind of intended, especially since isRunning was true. Link to comment Share on other sites More sharing options...
Recommended Posts