Doug Posted March 9, 2018 Share Posted March 9, 2018 Hi All I'm writing a basic platformer which has prizes that I'd like to be undulating in scale. The prizes are generated dynamically as the game progresses. I am using the following tween for this, newly assigning the tween to each new prize as it is created: this.tweens.add({ targets: [scObjCreated], scaleX: '-=.2', scaleY: '-=.2', duration: 700, ease: 'Sine.easeInOut', repeat: -1, yoyo: true }); However, this seems inefficient as I know that you can have one tween with multiple targets, so having loads of tweens that all do the same thing seems like poor practice. I tried assigning the tween to a variable as follows: this.MyTween = this.tweens.add({ targets: [scObjCreated], scaleX: '-=.2', scaleY: '-=.2', duration: 700, ease: 'Sine.easeInOut', repeat: -1, yoyo: true }); ...and then pushing the newly created prize objects to the "targets" array as follows: this.MyTween.targets.push(myNewObject); However, this doesn't seem to do anything. Is there a way to do this please? Thank you! Link to comment Share on other sites More sharing options...
samid737 Posted March 11, 2018 Share Posted March 11, 2018 You could re-target tweens by editing the tweenData directly: http://jsfiddle.net/0rg0tcw8/4/ Adding targets should be possible too by fiddling with the tweenData, but I think you have to manually keep track of tween.totalData (the length of the tween data array) , since totalData is cached when adding A tween. Link to comment Share on other sites More sharing options...
samme Posted March 11, 2018 Share Posted March 11, 2018 http://labs.phaser.io/edit.html?src=src\tweens\dynamic end property.js samid737 1 Link to comment Share on other sites More sharing options...
Doug Posted March 11, 2018 Author Share Posted March 11, 2018 Thanks both. @samme that's a nice example. Is there a way to actually add tween targets on the fly though? So create the tween and then add tween targets to that tween as the game progresses? Link to comment Share on other sites More sharing options...
samid737 Posted March 11, 2018 Share Posted March 11, 2018 Added the wrong fiddle (updated above): http://jsfiddle.net/0rg0tcw8/4/ Link to comment Share on other sites More sharing options...
Doug Posted March 11, 2018 Author Share Posted March 11, 2018 Thanks. This is more the kind of thing I was after, much appreciated. However, it seems be replacing the current target with the new one and then restarting rather than adding new targets to the existing tween. I assume that you can't just push to the targets array without restarting? Link to comment Share on other sites More sharing options...
samme Posted March 11, 2018 Share Posted March 11, 2018 Oh, I think I see now. If you're tweening in the same range (start–end) for all targets, you can just tween one dummy object and copy its values to the rest. But personally I would just use multiple tweens. They're meant to be disposable. Link to comment Share on other sites More sharing options...
Doug Posted March 12, 2018 Author Share Posted March 12, 2018 Thanks, that's great to know and much appreciated. Link to comment Share on other sites More sharing options...
Recommended Posts