Jump to content
Search Community

How to change the target variables during tween

Koshmaar test
Moderator Tag

Recommended Posts

Hey

 

I have a shuriken class which needs to fly to enemy, and this enemy is moving. So the target position needs to be updated in real time. I tried something like this:

 

  	 TweenLite.to(this, 0.35, { x: target.x, y: target.y,  
			   onUpdate: CorrectTargetPosition, onUpdateParams:["{self}"]} );

	public function CorrectTargetPosition( tween : TweenLite ) : void
	{	
	   tween.vars["x"] = target.x;
	   tween.vars["y"] = target.y;
	}

 

Unfortunately it doesn't work :) Values are changed, but it doesn't affect shuriken.

Link to comment
Share on other sites

Hi and Welcome to the GreenSock Forums.

 

TweenMax's updateTo() method allows you to change a tween's destination values while the tween is running: http://www.greensock.com/as/docs/tween/com/greensock/TweenMax.html#updateTo()

 

 

Although updateTo() will work, the DynamicProps plugin was created for exactly the use case you described where the target values are changing constantly. Please see the DynamicProps example in the interactive Plugin Explorer:

http://www.greensock.com/tweenmax

 

DynamicProps plugin docs: http://www.greensock.com/as/docs/tween/com/greensock/plugins/DynamicPropsPlugin.html

 

*note DynamicPropsPlugin is a Club GreenSock member benefit

Link to comment
Share on other sites

Thanks for the answer. Unfortunately currently I'm creating my first commercial game, and before it proves that it can pay for itself, I won't be buying any software. Also, I'm using TweenLite exlusively, because file size is very important to me ( yes, I know, 4kb is very little, but still I want to keep down unless absolutely no alternative ). If it's impossible to hack TweenLite, I'll have to hardcode this by myself :) not that difficult.

 

Btw, one more question... if I may. Is it possible to create a tween between 2 values that is not based on time ? I mean, instead of always tweening ie. for 2 seconds, the tween would take amount of time depending on the distance of those values.

 

An example would be, again a shuriken ;) that is flying with constant speed, regardless of the distance. Is it possible to create a tween like that?

Link to comment
Share on other sites

A tween by its very nature must have a duration, but it should be pretty simple to do the math and figure out what duration you need. For example:

 

function tweenTo(target:DisplayObject, vars:Object, speed:Number):TweenLite {
   var difference:Point = new Point( vars.x - target.x, vars.y - target.y); //difference on x-axis and y-axis
   var distance:Number = Math.sqrt(difference.x * difference.x + difference.y * difference.y);
   var duration:Number = distance / speed;
   vars.ease = Linear.easeNone; //ensures steady speed.
   return TweenLite.to(target, duration, vars);
}

//then, you can just do:
tweenTo(mc, {x:100, y:200}, 25); //animates to the new position at 25 pixels per second.

 

As for not spending any money, no problem. DynamicPropsPlugin is ideal for the scenario you mentioned, but if you can easily code it yourself and save the $, go for it. Beware, however, that it isn't exactly a trivial thing to be able to maintain the overall duration of a tween and alter the end position on the fly smoothly and also retain the easing properly, although a linear ease makes things much simpler.

 

Happy tweening.

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