Jump to content
Search Community

Providing a Tween with variables

tompins test
Moderator Tag

Recommended Posts

Hi All,

 

apologies if this has been asked before but it's causing me a headache. I am trying to provide a tween with variables (which are _x & _y coordinates) so here is my code:

 

 

if (id == 1) {

TweenLite.to(newPic,0.5,{_x:xcoordinate1, _y:ycoordinate1, onComplete:showText, ease:Circ.easeOut});

 

} else if (id == 2)

TweenLite.to(newPic,1,{_x:xcoordinate2, _y:ycoordinate2, onComplete:showText, ease:Circ.easeOut});

{

 

Where I know _x:xcoordinate1 are numbers. My problem is that when I run this code and toggle between these two tweens the values of the variables seems to be ADDED each time so that my image is gradually being pushed off the stage rather than toggle between two positions. (If I trace out the variable after each tween they are as expected but clearly the values are being incremented) I want the movieclip newPic to just move between the two points, does this make sense?

Many thanks

Link to comment
Share on other sites

It seems like xcoordinate1, ycoordinate1 etc probably are strings, which GSAP interprets as relative values. You can try the following to confirm that xcoordinate1 etc are definitely Numbers though

trace(typeof(xcoordinate1));

Values as strings are relative, while numbers are absolute. In v12 of GSAP though, this is deprecated (although still currently working). The preferred method of defining relative tweens in v12 is using a string in the form of "+=100" / "-=100".

 

Relative values are calculated as additions to the current values at the time the tween is created, which results in the additive effect it seems you are seeing. You shouldn't see this with absolute values. A simple way to make sure you are using absolute tweens if your variables are strings is casting them to numbers e.g.

TweenLite.to(newPic,0.5,{_x:Number(xcoordinate1), _y:Number(ycoordinate1)});
  • Like 2
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...