Jump to content

Tween.js - Performance tips?


Schoening
 Share

Recommended Posts

I am using https://github.com/sole/tween.js/ for tweening.

 

I am making a snake game. For every piece I am creating a tween:

function Tween ( array_index ) {  var i = array_index; // targets[ i ]  this.tween = new TWEEN.Tween( values[ i ] )    .to( targets[ i ] , 500)    .onUpdate(function(){    snake[ i ].position.x = this.x;    snake[ i ].position.y = this.y;    snake[ i ].rotation.z = this.rotation;    })    .onComplete(function(){      checkTweenCompletion();    })};

But that could look like it is insane when thinking about mobile performance...

 

Can you think of alternatives? Because I can't... Every piece of my snake has to move to the previous piece position. I don't think I can do that in a single tween.

Link to comment
Share on other sites

How many simultaneous tweens do you think you are talking about here? Unless there really are a lot of them I don't think it should actually be much of a problem. Have you done tests to check the performance impact? There are a few micro-optimizations you could do though. For example you could reduce impact on the garbage collector by re-using the same tween objects (ie assign one to each snake segment) rather than creating a new one each time you want to move. You could also cache values fetched from arrays in local variables like this

.onUpdate(function(){	var s = snake[ i ];    s.position.x = this.x;    s.position.y = this.y;    s.rotation.z = this.rotation;    })

but I still wouldn't expect to see a dramatic impact from these kinds of changes.

Link to comment
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...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...