mralanlee
mralanlee

Reputation: 489

Stopping all animation upon click-through in AS3

In AS2, I would just add a stop(); into the clickTag, but I'm not sure how I would do so in AS3.

Here's a sample code of my clickTag

package 
{
import com.greensock.*;
import com.greensock.easing.*;

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;

public class main300x250 extends MovieClip
{
public var paramList:Object;

    public function main300x250()
    {
        addEventListener(Event.ADDED_TO_STAGE, init);

    }

    private function init(e:Event):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        myBTN.addEventListener(MouseEvent.CLICK, clickHandler);
        myBTN.addEventListener(MouseEvent.ROLL_OVER, clickOver);
        myBTN.addEventListener(MouseEvent.ROLL_OUT, clickOut);


        TweenNano.from( t1, .5, {alpha:0, scaleX:0, scaleY:0, ease:Back.easeOut, delay:0});
        TweenNano.to( t1, .5, {scaleX:0, scaleY:0, ease:Back.easeIn, delay:1.75});

        TweenNano.from( f2.a, .35, {y:"+35", alpha:0, delay:2.25});
        TweenNano.from( f2.b, .35, {y:"+35", alpha:0, delay:2.4});
        TweenNano.from( f2.nugsL, .5, {x:"-200", ease:Quad.easeIn, delay:2.75});
        TweenNano.from( f2.nugsR, .5, {x:"+200", ease:Quad.easeIn, delay:3.15});

        TweenNano.to( f2.a, .25, {y:"+15", alpha:0, delay:5.5});
        TweenNano.to( f2.b, .25, {y:"+15", alpha:0, delay:5.65});

        TweenNano.from( f3.a, .35, {y:"+35", alpha:0, delay:5.75});
        TweenNano.to( f2.nugsL, .5, {x:6.5, y:30.5, ease:Quad.easeIn, delay:6.2});
        TweenNano.to( f2.nugsR, .5, {x:6.5, y:30.5, ease:Quad.easeIn, delay:6.2});
        TweenNano.from( f3.d, .5, {x:"-20", y:"+20", alpha:0, delay:6.6});

        TweenNano.to( f3.a, .25, {y:"+15", alpha:0, delay:8.75});
        TweenNano.to( f2.nugsL, .25, {x:"+350", delay:8.9});
        TweenNano.to( f2.nugsR, .25, {x:"+350", delay:8.9});
        TweenNano.to( f3.d, .15, {alpha:0, delay:9});           

        TweenNano.from(t2, 0.65, {delay:9.15, y:'+='+250, ease:Back.easeOut});

        TweenNano.from(t2.a, 0.2, {delay:9.25, y:'+='+75});
        TweenNano.from(t2.b, 0.2, {delay:9.45, y:'+='+75});
        TweenNano.from(t2.c, 0.2, {delay:9.45, y:'+='+75});

        TweenNano.to(t2.b, 0.25, {delay:10.2, x:"-30"});
        TweenNano.to(t2.c, 0.25, {delay:10.2, x:"-30"});
        TweenNano.to(t2.d, 0.25, {delay:10.2, alpha:1, x:"+31"});

        TweenNano.to(endLockUp, 1, {delay:10.2, scaleX:1.15, scaleY:1.15, ease:Quad.easeIn});
        TweenNano.from(endLockUp.a, 0.2, {delay:10.2, y:"+200"});
        TweenNano.from(endLockUp.b, 0.2, {delay:10.4, x:"-200"});
        TweenNano.from(endLockUp.c, 0.2, {delay:10.4, x:"+200"});
        TweenNano.from(endLockUp.d, 0.2, {delay:10.6, x:"-200"});
        TweenNano.from(endLockUp.e, 0.2, {delay:10.6, x:"+200"});
        TweenNano.from(endLockUp.f, 0.2, {delay:10.8, x:"-200"});
        TweenNano.from(endLockUp.g, 0.2, {delay:10.8, x:"+200"});
        TweenNano.from(endLockUp.h, 0.2, {delay:11, x:"-120"});
        TweenNano.from(endLockUp.i, 0.2, {delay:11, x:"+120"});
        TweenNano.to(endLockUp, 0.2, {delay:11.2, scaleX:1, scaleY:1});


        TweenNano.from( cta, .5, {y:"+200", ease:Back.easeOut, delay:11.4});

        TweenNano.from( logo, .15, {y:-62.65 , delay:11.85});
        TweenNano.from( legal, .1, {alpha:0, delay:11.9});



    }

    private function clickOver(e:MouseEvent):void
    {
        TweenNano.to( cta, .25, {scaleX:1.15, scaleY:1.15});
    }

    private function clickOut(e:MouseEvent):void
    {
        TweenNano.to( cta, .25, {scaleX:1, scaleY:1});
    }

    function clickHandler(e:MouseEvent):void {
        var click_url:String = root.loaderInfo.parameters.clickTAG;
        if(click_url) {
        navigateToURL(new URLRequest(click_url), '_blank');
        TweenNano.stop();
                        }
            }


        }

 }

The animation is nested into another .as file Edit: pasted entire .as file code.

Thanks in advance.

Upvotes: 2

Views: 395

Answers (1)

user2136963
user2136963

Reputation: 2606

This stops all animation:

package 
{
    import flash.display.MovieClip;
    import flash.display.DisplayObject;
    public function stop_animation(object:Object, checker:Function=null):void
    {
        if (checker != null)
        {
            if(!checker(object))
                return;
        }   
        if(!object)
            return;
        if(object as MovieClip)
            object.gotoAndStop(object.currentFrame);
        if(!object.hasOwnProperty("numChildren")||(! object as DisplayObject))
            return;
        for(var i:int=0;i<object.numChildren;++i)
            stop_animation(object.getChildAt(i));
    }
}

This stops all animation, which is under position of the mouse click: EDIT: Actually, it doesn't when clips are nested

package 
{
    import flash.display.MovieClip;
    import flash.display.DisplayObject;

    public function stop_animation(object:Object, stage:*, checker:Function=null):void
    {
        if (checker != null)
        {
            if(!checker(object))
                return;
        }   
        if(!object)
            return;
        if(object as MovieClip)
            if(object.hitTestPoint(stage.mouseX,stage.mouseY))
                object.gotoAndStop(object.currentFrame);
        if(!object.hasOwnProperty("numChildren")||(! object as DisplayObject))
            return;
        for(var i:int=0;i<object.numChildren;++i)
            stop_animation(object.getChildAt(i),stage);
    }
}

and can be called like: stop_animation(this,stage); If you pass a checker function, all objects for which it returns false will be excluded from applying the functions.


This probably won't do with arrays, Dictionaries and etc., but you can iterate through them and call it from current element. (Supposing, that they have no inner arrays/dictionaries)


Code that should stop greensock animation:

    package 
{
    import flash.events.Event;
    import com.greensock.*;
    import com.greensock.easing.*;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class main extends MovieClip
    {
        var is_stopped:Boolean=false;

        var tweens:Array=new Array();
        public function main()
        {
            addEventListener(Event.ADDED_TO_STAGE,setup);
            addEventListener(MouseEvent.MOUSE_DOWN, clickHandler);
        }
        public function setup(e:Event):void
        {
            tweens.push(TweenNano.from( t1, .5, {alpha:0, scaleX:0, scaleY:0, ease:Back.easeOut, delay:0}));
            tweens.push((TweenNano.to( t1, .5, {scaleX:0, scaleY:0, ease:Back.easeIn, delay:1.75})));

        }
        public function clickHandler(e:MouseEvent):void
        {
            trace("!");
            is_stopped=true;
            t1.stop();
            for(var i in tweens)
            {
                tweens[i].kill();
            }
        }
    }
}

CLICK sometimes behaves incorrectly, so I replaced it with DOWN

Upvotes: 1

Related Questions