Jump to content
GreenSock

Amnesicat

Multiple tweens with reverse toggle

Recommended Posts

Hi everybody,

 

I would like to tween multiple movieclips (I’m only using 3 for this example) separately using only one line of tween code, but also make the targeted movieclip reverse its tween when it’s clicked on again. I’m not entirely sure how to do this as I’m not really proficient at AS3. I know I could use an eventlistener and tween for each movieclip but I would rather have something more efficient with less code clutter.

 

Here’s what I’ve come up with so far. This code tweens each movieclip one way but doesn’t want to reverse when clicked on again.

 

import com.greensock.*;
import com.greensock.easing.*;
import flash.events.MouseEvent;

var mcArray:Array = [mc1, mc2, mc3];

for (var i:int = 0; i < mcArray.length; i++)
{
mcArray[i].addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true);
}

function clickHandler(e:MouseEvent):void
{
var mcTween:TweenMax = TweenMax.to(e.target, 1, {y:"200", ease:Quad.easeInOut, paused:true, reversed:true});
mcTween.reversed = ! mcTween.reversed;
mcTween.resume();
}

Any suggestions or guidance would be greatly appreciated.

Thanks,

Alan

Link to comment
Share on other sites

the reason your tweens weren't reversing is because everytime you clicked you created a new tween.

the trick is to give each movie clip its own tween and only create it once.

 

try this:

 

import com.greensock.*;
import com.greensock.easing.*;
import flash.events.MouseEvent;

var mcArray:Array = [mc1, mc2, mc3];

for (var i:int = 0; i {
  mcArray[i].addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true);
  //give each mc its own tween
  mcArray[i].tween = TweenMax.to(mcArray[i], 1, {y:"200", paused:true, reversed:true});
}

function clickHandler(e:MouseEvent):void
{
 //tell the clicked mc to reverse its own tween
 e.target.tween.reversed = ! e.target.tween.reversed;
 e.target.tween.resume();
}

Link to comment
Share on other sites

Thanks a million Carl. I knew there would be a logical solution. Gee, there's so much to learn with this actionscript stuff, just not enough hours in the day.

Thanks again,

Alan

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