Jump to content
Search Community

Timeline Problem. (not getting updated position values)

yukamui test
Moderator Tag

Recommended Posts

Hello everyone, thanks for checking this.

 

My English is not very good but I will try my best to explain my problem. I want to make a mc move in reaction to another, but when I try to use the x position of a mc

that was moved earlier in the timeline its position has not changed, I have even traced the position with the addCallback function and it gives me the same position

at the start and at the end of the timeline. Do you know what am I doing wrong? Or any way to achive this?

 

Thankyou very much!

 

var tl:TimelineMax= new TimelineMax;

tl.addCallback(trace,tl.duration,['mc2.x='+mc2.x]); //The X of mc2 is the same here than at the end of the Timeline.
tl.append(TweenMax.to(mc1,1,{x:mc2.x}));
tl.append(TweenMax.to(mc2,1,{x:mc2.x+mc2.width})); //Eventough I have moved its position here. 
tl.append(TweenMax.to(mc1,1,{x:mc2.x}));
tl.addCallback(trace,tl.duration,['mc2.x='+mc2.x]);  //The X of mc2 is the same here than at the start of the Timeline.

Link to comment
Share on other sites

Hi Yukamui,

 

Thank you for your question. I understood it fine yet it left me a little puzzled in finding a solution.

 

what you are probably realizing is that when you do something like:

tl.addCallback(trace,tl.duration,['mc2.x='+mc2.x]);

 

even though the callback doesn't happen until a few seconds into the timeline, the params that are used are "recorded" as soon as the timeline is built.

 

also when you do something like:

 

tl.append(TweenMax.to(mc1,1,{x:mc2.x}));

the value of mc2.x is recorded as soon a that code is processed, not when the timeline encounters that particular tween during playback.

 

Think of it this way. Suppose I want to give you directions to where I am. I might say:

 

go left out of your house

go 3 miles

make a right onto Helm Street

go 3 more blocks, I'll be at the starbucks

 

at the time I give you those directions (right now) those directions are 100% accurate.

 

let's imagine that 2 minutes into your journey I get hungry and go to McDonalds (changing my x and y coordinates)

 

what happens? you don't find me.

 

TimelineMax works in similar fashion, It executes the tweens based on the values it gets when the timeline is created.

 

run this code and see what happens:

 

import com.greensock.*;
var tl:TimelineMax= new TimelineMax;

tl.addCallback(debug,tl.duration,['mc2.x='+mc2.x]); 
tl.append(TweenMax.to(mc1,1,{x:mc2.x}));
tl.append(TweenMax.to(mc2,1,{x:mc2.x+mc2.width})); 
tl.append(TweenMax.to(mc1,1,{x:mc2.x}));

tl.addCallback(debug,tl.duration,['mc2.x='+mc2.x]); //mc2.x is recorded as soon as this line processed

function debug(output:String):void{
trace("output: " + output);
trace("current x: " + mc2.x);
}

 

you will see that when debug runs the second time

output = the value of mc2.x that existed when the timeline was created

current x = mc2.x value at the time the callback was executed

 

------

 

what we need to do is force a particular tween to get new destination values every time it starts to fix your problem.

in the case of my directions imagine if i told you:

 

go left out of your house

go 3 miles

make a right onto Helm Street

go 3 more blocks and then use the short-range gps app i installed on your phone that can pinpoint my location within 300 yards.

 

now try this code:

 

import com.greensock.*;

//create a tween with an onStart callback
var specialTween:TweenMax = TweenMax.to(mc1,1,{x:mc2.x, onStart:resetSpecialTween});

var tl:TimelineMax= new TimelineMax;

tl.append(TweenMax.to(mc1,1,{x:mc2.x}));
tl.append(TweenMax.to(mc2,1,{x:mc2.x+mc2.width}));
tl.append(specialTween); // add the special tween

//every time specialTween starts it will get the current value of mc2.x
function resetSpecialTween(){
trace("init special gps app  |  mc2.x = " + mc2.x);
specialTween.setDestination("x", mc2.x);
}

 

*in order to use setDestination make sure you have the latest greensock files

 

It actually took me a few minutes to figure out the solution. There may be a better approach, but currently I am at a loss. hopefully my explanation and solution will be suitable.

 

carl

Link to comment
Share on other sites

Thank you for your fast reply Carl! You have helped me a lot with your great explanation. Your solution is great and it have pointed me in the direction of the .updateTo(); and the DynamicPropsPlugin; So before applying it I will study the .updateTo(); I am not a club member yet, so I won't be able to try the DynamicPropsPlugin, but if a Member have this situation he might try it.

 

Tonight when I get home I will apply what you have taught me here, thanks again for sharing your knowledge, people like you rock!

Link to comment
Share on other sites

glad it helped.

 

just 2 things:

 

setDestination() replaces updateTo(), so it is recommended that you use setDestination().

 

***EDIT I'm completely wrong, updateTo replaces setDestination *****

 

DynamicPropsPlugin is great for when the destination values are constantly changing. In your case finding the new destination values at the beginning of the tween will be sufficient in your situation.

Link to comment
Share on other sites

I was checking the documentation and found the following:

 

setDestination(property:String, value:Boolean, adjustStartValues:* = true):void

Adjusts a destination value on the fly, optionally adjusting the start values so that it appears to redirect seamlessly without skipping/jerking (this method has been deprecated in favor of updateTo()).

 

That's why I tought it was the other way arround...

 

I tryed your solution with the updateTo() method and it worked as fine. Now I am trying to use the getActive(); in an atempt not to have to define the variable of the specialTweens... but have not been succesful on that yet.

 

Thank you again for following this thread.

 

 

//create a tween with an onStart callback
var specialTween:TweenMax = TweenMax.to(mc1,1,{x:mc2.x, onStart:resetSpecialTween});
var tl:TimelineMax= new TimelineMax;

tl.append(TweenMax.to(mc1,1,{x:mc2.x}));
tl.append(TweenMax.to(mc2,1,{x:mc2.x+mc2.width}));
tl.append(specialTween); // add the special tween

//every time specialTween starts it will get the current value of mc2.x
function resetSpecialTween(){
  trace("init special gps app  |  mc2.x = " + mc2.x);
 specialTween.updateTo({x:mc2.x},false);
 // specialTween.setDestination("x", mc2.x);
}

Link to comment
Share on other sites

No, thank you for all the help you are giving me. I hope I am not getting too abusive with so many questions but I have this new situation if you don't mind looking at it. I managed to use the getActive() to avoid the creation of the variable, using the one that tweenlinemax creates, and everything works fine.... but when I try to use it twice, the second time it does not update the values... I tried to explain it on the trace statements...

 

If you have any thoughts on this, I would love to read them.

 

 

import com.greensock.TweenMax;
import com.greensock.TimelineMax;
import com.greensock.*;

var tl:TimelineMax= new TimelineMax;

tl.insert(TweenMax.to(mc1,1,{x:mc2.x}),tl.duration); //Moves the mc1 to the mc2 position.
tl.insert(TweenMax.to(mc2,1,{x:mc2.x+mc2.width}),tl.duration); //The mc2 moves its width to the right.
tl.insert( TweenMax.to(mc1,1,{x:mc2.x,onStart:resetSpecialTween,onStartParams:[tl.getActive()[0]]}),tl.duration); // add the special tween, which now perfectly follows to the new x of Mc2
tl.insert(TweenMax.to(mc2,1,{x:mc2.x+mc2.width,onStart:resetSpecialTween,onStartParams:[tl.getActive()[0]]}),tl.duration); //Here I got the new problem, I thought that here I would reset the values again... but that does not happen.
tl.insert( TweenMax.to(mc1,1,{x:mc2.x,onStart:resetSpecialTween,onStartParams:[tl.getActive()[0]]}),tl.duration); // add the special tween So when I move the mc1 again to follow mc2, it goes back to the later position.

//every time specialTween starts it will get the current value of mc2.x
function resetSpecialTween(specialTween:TweenMax){
specialTween.updateTo({x:mc2.x},true);
trace(mc2.x) //This function is called 3 times, everytime this trace gives the same position.
}

Link to comment
Share on other sites

pretty clever solution of using getActive() and no you are not being abusive by any means.

 

try this:

 

import com.greensock.*

var tl:TimelineMax= new TimelineMax;

tl.insert(TweenMax.to(mc1,1,{x:mc2.x}),tl.duration); //Moves the mc1 to the mc2 position.
tl.insert(TweenMax.to(mc2,1,{x:mc2.x+mc2.width}),tl.duration); //The mc2 moves its width to the right.
tl.insert( TweenMax.to(mc1,1,{x:mc2.x,onStart:resetSpecialTween,onStartParams:[tl.getActive()[0]]}),tl.duration); 
tl.insert(TweenMax.to(mc2,1,{x:String(mc2.width)}),tl.duration); //will move to the right a distance equal to mc2's width
tl.insert( TweenMax.to(mc1,1,{x:mc2.x, onStart:resetSpecialTween,onStartParams:[tl.getActive()[0]]}),tl.duration); 

//every time specialTween starts it will get the current value of mc2.x
function resetSpecialTween(specialTween:TweenMax){
trace(tl.getActive()[0].target.name);
   specialTween.updateTo({x:mc2.x},true);
   trace(mc2.x)
}

 

I think that basically works BUT you will notice perhaps that the last tween starts in the wrong direction and then very quickly changes direction. I'm not exactly sure why that is or how to explain it accurately :shock: perhaps you can start the last tween with a higher x destination so atleast it is moving in the same direction before the updateTo happens.

 

as for one problem in your recent code, consider the following:

 

tl.insert(TweenMax.to(mc2,1,{x:mc2.x+mc2.width,onStart:resetSpecialTween,onStartParams:[tl.getActive()[0]]}),tl.duration); 

 

so you are saying, move mc2 to a certain value based on mc2 current x position and width, and then immediately onStart you overwrite those values and say updateTo({x:mc2.x}). Well mc2.x is exactly at mc2.x so that tween no longer does anything once the onStart happens. follow? notice in my code above that I passed in a string value for the width of mc2. that means it will move relative to its current position. seems to do the job.

 

I wish I had more elegant solutions for you.

Link to comment
Share on other sites

Great!!! That should do the trick! Thanks for pointing me out the problem, using a String is a great solution.

I changed the code earlier but was stuck with the same problem,but by applying the String it was solved! The problem with the quickly change of direction was made by the

updateTo() function, sending true as a parameter would reset the duration making it skip, just changing that to false made it run smoothly... thanks again you have been of great help.

 

Finally what I got was:

 

import com.greensock.TweenMax;
import com.greensock.TimelineMax;
import com.greensock.*;
import flash.display.MovieClip;

var tl:TimelineMax= new TimelineMax;
tl.insert(TweenMax.to(mc1,1,{x:mc2.x}),tl.duration);
tl.insert(TweenMax.to(mc2,1,{x:mc2.x+mc2.width}),tl.duration);
tl.insert( TweenMax.to(mc1,1,{x:mc2.x,onStart:resetSpecialTween,onStartParams:[tl,mc2]}),tl.duration);
tl.insert(TweenMax.to(mc2,1,{x:String(mc2.width)}),tl.duration); //Here I applied what you suggested.
tl.insert( TweenMax.to(mc1,1,{x:mc2.x,onStart:resetSpecialTween,onStartParams:[tl,mc2]}),tl.duration); //I even repeated the lines several times without problems.
tl.insert(TweenMax.to(mc2,1,{x:String(mc2.width)}),tl.duration);
tl.insert( TweenMax.to(mc1,1,{x:mc2.x,onStart:resetSpecialTween,onStartParams:[tl,mc2]}),tl.duration);
tl.insert(TweenMax.to(mc2,1,{x:String(mc2.width)}),tl.duration); //he he, kind of looks like a worm huh?

function resetSpecialTween(tl:TimelineMax,mc:MovieClip){ //I was sending an mc as a parameter because I thought I might need to reset mc1.... but didn't do it.
var aux:TweenMax = tl.getActive(false,true,false)[0];
aux.updateTo({x:mc.x},false);//If this parameter is true, then you should see the quickly change of direction that you told me.
}

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...