CreateJS / TweenJS

A simple but powerful tweening / animation library for Javascript. Part of the CreateJS suite of libraries.
http://createjs.com/
MIT License
3.56k stars 967 forks source link

Delayed Call order is backwards? #34

Closed aholla closed 7 years ago

aholla commented 10 years ago

Hi, if i set a bunch of delayed called, they appear to return in the incorrect order:

p.loop = function () {
for (var i = 0; i < 10; i ++) {
    // override can be true or false
    createjs.Tween.get( {}, {override : false} )
        .wait(  1000 )
        .call( this.printID, [ i ], this )
    }
}

p.printID = function ( id ) {
    console.log( id )
}

This then outputs:

9
8
7
6
5
4
3
2
1
0

This is wrong right? First in should be first out.

If I were to use TweenMax with the following code:

TweenMax.delayedCall( 1.0, this.printID, [ i ], this );

the results would be:

0
1
2
3
etc...
lannymcnie commented 10 years ago

Thanks, I will check this out.

gskinner commented 10 years ago

We currently iterate the tween list in reverse in order to avoid race conditions when a tween removes itself when it completes.

If this is a major issue, we could potentially duplicate the array and iterate normally. This would also solve edge case race conditions (like one tween removing an older tween within a tick). We currently do this in EaselJS (due to the higher chance of race conditions), and could definitely do it here, it just has a bit larger performance cost, and also has GC implications (because you're generating and populating a throw-away array each frame).

Thoughts?

aholla commented 10 years ago

Hmmm. I personally would prefer the the delayed call to come out in the order it went in but I understand the problem. I guess every tween and Movieclip would have to perform these operations which does seem pointless so some checks would be needed but these should be quite cheap.

You could perhaps only perform the reverse if the is a 'onComplete' callback present. Or pass another param to the onComplete to enable the reverse sort.

I think if you do an 'onComplete', it should check the object for other tweens and do the reverse if it finds any others. It might be a small price to pay but I think the the output should be in the correct order.

In my example I am am incrementally tweening a 'score' bar and could not figure out a way to do it because the order is reversed. I had to switch to TweenMax.

Thanks for looking into this, having 2 tweening libraries in my project is s little messy :)

aholla commented 10 years ago

An couple of other options would be to change the way the Tweens are inserted into the array, say use "unShift" to add the Tween at the begging. This would obviously be slower than a push but I don't know if it would be faster that a re-sort and GG?

Also a DoubleLinkedList could solve the speed issues.

gskinner commented 7 years ago

We have switched to a double linked list implementation, which solved this one.