Dream Of Sleeping

Can I and should I reuse Tweens?

Recommended Posts

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?

 

 

 

Share this post


Link to post
Share on other sites

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 ;)

 

Regards

George

Share this post


Link to post
Share on other sites

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 factory

tween = game.add.tween(...)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.