Jump to content
Search Community

Great Tweening Engine!!! Have some questions..

DigitalOne test
Moderator Tag

Recommended Posts

First of all,

 

I would like to commend Jack on a great set of classes!! I've found my new tweening engine ;) Thank you so much for making the core assets available to us.

 

I have a question about syntax. I'm wondering exactly when to use "new" and I'm kinda confused. In the following code, a "new" constructor (am I saying this right?) is being used:

 

var myTimeline:TimelineLite = new TimelineLite({paused:true});
myTimeline.append( new TweenLite(mc, 1, {x:100, ease:Back.easeIn}) );
myTimeline.append( new TweenLite(mc, 1, {y:200, ease:Back.easeIn}));
myTimeline.append( new TweenMax(mc, 1, {tint:0xFF0000}), -2 )

 

However, when using "TweenMax.from" or "TweenLite.from" when appending the TimeLine, "new" cannot be used as shown here: (i get an error if i use "new")

 

myTimeline.append(TweenLite.from(box1, 1, {x:0, ease:Back.easeIn}) );
myTimeline.append(TweenLite.from(box2, 1, {x:0, ease:Back.easeIn}), -1);
myTimeline.append(TweenMax.from(box1, 1, {tint:0xFF0000}), -2 );

myTimeline.addLabel("spin", 2);
myTimeline.insertMultiple( TweenMax.allTo([box1, box2], 5, {rotation:"360", ease:Back.easeIn}), "spin");

 

Why is this?

 

Thank you

-Digital

Link to comment
Share on other sites

Good question. Basically, these lines produce exactly the same results:

 

myTimeline.append( TweenLite.to(mc, 1, {x:100}) );
myTimeline.append( new TimelineLite(mc, 1, {x:100}) );

 

Some people like to use the "new TweenLite(...)" syntax and others prefer the static "TweenLite.to(...)" call. However, you asked about the static from() call. The only difference with the from() call is that it automatically sets the "runBackwards" and "immediateRender" special properties to true, so these lines produce the same results:

 

myTimeline.append( TweenLite.from(mc, 1, {alpha:0}) );
myTimeline.append( new TweenLite(mc, 1, {alpha:0, runBackwards:true, immediateRender:true}) );

 

You're welcome to omit the immediateRender:true if you don't want the tween to render in its starting state immediately by the way.

 

Does that clear things up?

 

Basically, the benefit of using the static to() and from() methods are twofold:

 

1) If you're just firing off individual tweens, they make it easier (especially for newbies) to avoid garbage collection issues because you're not storing references to your tweens in variables which could keep them from getting garbage collected (this has nothing to do with the tweening engine - it's just how AS3 and Flash's garbage collection routine works with all objects).

 

2) Some people find them more intuitive, especially doing "from(...)" instead of remembering to add runBackwards:true and immediateRender:true in new instances.

 

Again, it's totally up to you. Technically the "new TweenLite(...)" is VERY slightly faster, but I'd be shocked if the difference was ever noticeable even under extremely heavy loads, like thousands of tweens being created at the same time. The speed difference really shouldn't be a factor - I only mention it because some folks like those little nuggets of info and they are speed freaks. :)

Link to comment
Share on other sites

Thank you Jack for your reply,

 

So, you're saying that by using

myTimeline.append( new TimelineLite(mc, 1, {x:100}) );

a variable is being created that can be garbage collected. Other than declaring a new timeline object, which has to be done rather using static methods or not, where is the variable declaration in the above code? Is the variable created as some unseen reference for the compiler because of "new" ?

 

Sorry, I'm just trying to get my head around some of the aspects of OOP. Thank you for taking the time to respond.

-D

Link to comment
Share on other sites

No no, sorry - my comment wasn't directly related to this context where you're putting tweens into a TimelineLite/Max. I was speaking generally. It actually makes zero difference whatsoever when you're putting them into a TimelineLite/Max because there's an internal reference stored, and removed by default when the timeline finishes. I was talking about if a user did this in a class or a MovieClip timeline:

 

var myTween:TweenLite = new TweenLite(mc, 1, {x:100});

 

See how he's storing a reference of the tween in a variable named "myTween"? If that's in a class-level variable or on a MovieClip timeline, that variable persists, meaning it won't be allowed to be garbage collected (again, this is a Flash issue and has nothing to do with the tweening engine itself). If you declare your variables as local ones inside a function, you're fine - those are only temporary inside the function anyway. But my point was that newbies don't always understand that stuff - they have no idea that storing a reference in a variable prevents gc of that object. So using TweenLite.to() or TweenLite.from() shields them from that issue.

 

Make more sense now?

Link to comment
Share on other sites

...that storing a reference in a variable prevents gc of that object. So using TweenLite.to() or TweenLite.from() shields them from that issue.

 

Ok, bear with me here.

 

Neither

myTimeline.append( new TweenLite(mc, 1, {y:200, ease:Back.easeIn}));

or

myTimeline.append(TweenLite.from(box1, 1, {x:0, ease:Back.easeIn}) );

uses a variable to store the tween. So how is the user being shielded from a variable being gc'd when no variable is present?

 

Again, not trying to be difficult, just trying to understand. 8-)

Link to comment
Share on other sites

Notice I said:

 

my comment wasn't directly related to this context where you're putting tweens into a TimelineLite/Max. I was speaking generally. It actually makes zero difference whatsoever when you're putting them into a TimelineLite/Max

 

And the example I gave where it DID matter was:

 

var myTween:TweenLite = new TweenLite(mc, 1, {x:100});

 

So it only matters when you are storing them in variables when creating tweens. It's still possible to do that when you're working with timelines:

 

var myTween:TweenLite = new TweenLite(mc, 1, {x:100});
myTimeline.insert(myTween);

 

See what I mean now?

Link to comment
Share on other sites

Ok...thank you.

 

I think the issue is with me and how I perceive objects/classes/methods as they relate to OOP.

 

So when you create a

var MyTween:TweenLite

that is an object, correct?

and

new TweenLite

is the constructor?

 

Ok, I realize I'm getting off topic now...sorry.

 

I went back and re-read your responses to me. I realize that variables declared within functions will expire (get garbage collected) after the function runs. However, when declared at the top level, i.e. not within ANY function, the variable will persist and cannot be garbage collected.

Link to comment
Share on other sites

This does not create an object:

 

var MyTween:TweenLite

 

It just declares a variable which is like a container for an object. But you haven't put anything into that container yet. The "new TweenLite(...)" is the actual object. And yes, that line calls the constructor (which is like the function in the class that gets called when the instance is created)

 

Clearing things up?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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