Dream Of Sleeping Posted March 19, 2014 Share Posted March 19, 2014 Hello. Sorry for all the questions. I'm trying to pay it forward by helping people when I can. Which I admit isn't all that often but it's getting more common. You know those little pop up scores most games have that go "+50" then rise and disappear. I'm making my game have those. And it will have a lot of them! At the moment I'm using game.add.tween. Which seems to create a new one each time. Is there a way to reuse them? If not will this have an impact on performance? I have my own pop up score class anyway, and it would be no bother to create it's own function that tweens it without using an actually tween. Also, slightly related. I noticed that Texts don't have an alive property. Yet they can be added to groups. Should they not have an alive property for methods like getFirstDead(). I see that when added to a group it gets it's alive set to true, which I know creates a propperty but isn't it better for objects to have their properties defined at the beginning? michaelcalkins and kstamatov85 2 Link to comment Share on other sites More sharing options...
Dream Of Sleeping Posted March 19, 2014 Author Share Posted March 19, 2014 I've figured it out. I'll post here for anyone searching the same question. I just created the tween like this. this.tween = new Phaser.Tween(this, this.game, this.game.tweens); Then I just reused it. Now I feel stupid. haha Link to comment Share on other sites More sharing options...
george Posted March 19, 2014 Share Posted March 19, 2014 Regarding you tweening question. I think this is a classical example of premature optimisation Just do it like you would normally do. Create a tween anytime you need to and only fix it if you have a performance problem. But I do not see any problem in creating many tweens, it's probably more time-consuming to render all the involved graphics. A tween removes itself from the TweenManager after it's completion, so you also do not have to worry about the memory consumption of old tweening instances. You could try another solution within your popup class:Use the update method of your sprite and update the position and scale manually if the animation itself is not too complex. You would save each tween instance. Plus: I would not call it premature optimisation in this case RegardsGeorge kstamatov85 and michaelcalkins 2 Link to comment Share on other sites More sharing options...
george Posted March 19, 2014 Share Posted March 19, 2014 Oh I see your solution now. So there is only a single popup involved ? Than reusing is definitely your way to go. I thought of many many popups scattered all over your stage. To store a reference to the tween (to stop & start) you can still use the factorytween = game.add.tween(...) Link to comment Share on other sites More sharing options...
Dream Of Sleeping Posted March 19, 2014 Author Share Posted March 19, 2014 Thanks for the reply. There tends to be about 4 or 5 popups at a time. The tweens are created inside my own popup text class. Actually I call it FloatyText. haha Link to comment Share on other sites More sharing options...
rich Posted March 19, 2014 Share Posted March 19, 2014 If creating the tweens is becoming an issue then I would use Tween.generateData and apply it manually to the Sprites. This saves having to generate the tween data (and run time comparisons) every frame. Have a look at Examples/tweens/generate data.js to see how it's done. clark and hellspawn_bg 2 Link to comment Share on other sites More sharing options...
Dream Of Sleeping Posted March 19, 2014 Author Share Posted March 19, 2014 OK thanks. I don't know whether it's becoming an issue or not. I know so little about optimisation. Sometimes I hear people saying you should reuse objects whenever possible, so I thought it might be a problem generating that many tweens. Link to comment Share on other sites More sharing options...
hellspawn_bg Posted September 17, 2014 Share Posted September 17, 2014 That's nice, but the example says that the index advance in the update method is crude and does not take the target device speed into account at all. Does it mean that on slower devices the tween will appear slower and on faster ones - faster? Link to comment Share on other sites More sharing options...
rich Posted September 17, 2014 Share Posted September 17, 2014 So use a better method of advancing the index?The issue here was the creation of tween objects which data generation avoids. Link to comment Share on other sites More sharing options...
hellspawn_bg Posted September 17, 2014 Share Posted September 17, 2014 I am sorry, I am new to phaser. Is there any method for index advance that takes in account the device speed. Link to comment Share on other sites More sharing options...
hollowdoor Posted February 9, 2015 Share Posted February 9, 2015 Edit: Never mind. I just thought of a way to do it. I can dispatch a user made phaser signal in the tween.onComplete listener. I'm already using that method somewhere else. duh As a side benefit this solution could also solve Dream Of Sleeping's problem in a round about way. Here's how to do it.function CustomSprite(game, x, y, name, frame){ Phaser.Sprite.call(game, x, y, name, frame); this.moving = false; this.canMove = true; //Add listeners with events.onStill.add anywhere. this.events.onStill = new Phaser.Signal();}CustomSprite.prototype = Object.create(Phaser.Sprite.prototype);CostomSprite.prototype.constructor = CustomSprite;CustomSprite.prototype.update = function(){ //An example move. var move = {x: 100, y: 200}; //Only one check even though there are still multiple tweens being created. if(this.alive && !this.moving && this.canMove){ var tween = game.add.tween(this).to(move, 1000 * 1, Phaser.Easing.Cubic.In, true); this.moving = true; this.canMove = false; tween.onComplete.addOnce(function(){ this.moving = false; //Set canMove somewhere else. this.events.onStill.dispatch(this); }, this); }};CustomSprite.prototype.activate = function(){ //A less automated approach. //Instead use a method that can be called on other object's signals. //An example move. var move = {x: 100, y: 200}; var tween = game.add.tween(this).to(move, 1000 * 1, Phaser.Easing.Cubic.In, true); this.moving = true; tween.onComplete.addOnce(function(){ this.moving = false; this.events.onStill.dispatch(this); }, this); };Original Post:Even though I have a different reason I would also like to reuse tweens. I have up to 40 moving sprites possibly using the same tween at different times. The moving sprites interact with another unknown number of sprites when onComplete. Because I don't know how many dependent sprites there's going to be I'd like to set the onComplete for the moving sprites in the dependent sprites constructor. Otherwise there is one signal, and I'll have to loop all the moving sprites to detect for dependent sprites, and then do an event by code logic. I know the types of events. For instance overlapping is one. I think you can see my dilemma. Why use the signal in the first place. I might as well be using the update function, but that would require multiple checks on every update which is overhead I'd rather not have. A group won't solve the problem either because I'd still have to use the same method to distribute checks. I'll try to see if I can reword the problem, and use a special topic for this since it's not exactly the same as this topic. Link to comment Share on other sites More sharing options...
Recommended Posts