AS3如何从时间轴中操纵类的不同实例

时间:2011-02-17 21:06:55

标签: actionscript-3 class

我正在尝试使用as3创建菜单,我似乎无法找到完成它的方法。 this is what i got so far

所以我写了一个附加到movieclip的类,我在舞台上放了几个实例。

package {

import flash.events.Event;
import flash.events.MouseEvent;
import fl.transitions.Tween;
import fl.transitions.TweenEvent; 
import fl.transitions.easing.*;
import flash.display.*;

public class ServicesButtons extends MovieClip {

    public function ServicesButtons() {

        shapemc.width = 0;

        this.addEventListener(MouseEvent.ROLL_OVER, mouseOn);
        this.addEventListener(MouseEvent.ROLL_OUT, mouseOff);
        this.addEventListener(MouseEvent.CLICK, clicked);

        function mouseOn(e:Event) {
            var shapeGrow:Tween = new Tween(shapemc, "width", Strong.easeOut, 0, 200, .3, true);
        }

        function mouseOff(e:Event) {
            var shapeShrink:Tween = new Tween(shapemc, "width", Strong.easeOut, shapemc.width, 0, .3, true);
        }

        function clicked(e:Event){
            removeEventListener(MouseEvent.ROLL_OUT, mouseOff);     
            removeEventListener(MouseEvent.ROLL_OVER, mouseOn);
        }

    }

}

}

我希望动画在点击某个按钮后不会回滚,我希望动画在我点击其他按钮后再返回。

我的问题是,我如何从这里开始,我在网上搜索线索,从我理解我可以在类中设置变量并在时间轴上更改它们并使用事件监听器来捕获更改。我试过这样做,但我总是以一些让我无处的冗余方式结束......

1 个答案:

答案 0 :(得分:0)

我必须保留之前点击的按钮:

public class ServicesButtons extends MovieClip {

    private static var activeButton : ServicesButtons;

    public function ServicesButtons() {
        setListener();
        this.addEventListener(MouseEvent.CLICK, clicked);
    }

    private function mouseOn(e:Event) {
        var shapeGrow:Tween = new Tween(shapemc, "width", Strong.easeOut, 0, 200, .3, true);
    }

    private function mouseOff(e:Event) {
        var shapeShrink:Tween = new Tween(shapemc, "width", Strong.easeOut, shapemc.width, 0, .3, true);
    }

    private function clicked(e:Event){
        if(activeButton != this)
        {
            if(activeButton)
                activeButton.setListener();
            setListener();
            activeButton = this;
        }
    }

    public  function setListener() : void{
            if(hasEventListener(MouseEvent.ROLL_OUT))
            {
                removeEventListener(MouseEvent.ROLL_OUT, mouseOff);     
                removeEventListener(MouseEvent.ROLL_OVER, mouseOn);
            }else{

                addEventListener(MouseEvent.ROLL_OVER, mouseOn);
                addEventListener(MouseEvent.ROLL_OUT, mouseOff);
                mouseOff(null);
            }

    }

}