操作脚本问题:添加/删除子项



我有三个影片剪辑都链接到舞台,我希望它们的行为像一个按钮/但我没有使用按钮,因为我还没有找到一种方法来让每个部分(向上、向上、向下、点击)进行动画处理,而不仅仅是在鼠标使用时进行更改。到目前为止,我已经能够让这三个都出现在我的舞台上,并在鼠标悬停时和单击时显示,但我认为我在 removeChild 方面做错了什么。每个 MC 应该一次出现一个,现在当我将鼠标悬停在上面并且似乎"闪烁"时,所有三个都会出现。这是我的代码:

var mainMoon:swayingMoon = new swayingMoon();
mainMoon.x = 50;
mainMoon.y = 10;
addChild(mainMoon);
var hoverMoon:glowMoon = new glowMoon();
hoverMoon.x = 50;
hoverMoon.y = 10;
var movieMoon:clickedMoon = new clickedMoon();
movieMoon.x = 50;
movieMoon.y = 10;
mainMoon.addEventListener(MouseEvent.ROLL_OVER, showHoverMoon);
mainMoon.addEventListener(MouseEvent.ROLL_OUT, hideHoverMoon);
hoverMoon.addEventListener(MouseEvent.CLICK, startMovieMoon)
function showHoverMoon(event:MouseEvent):void
{
 addChild(hoverMoon);
}
function hideHoverMoon(event:MouseEvent):void
{
removeChild(hoverMoon)
}
function startMovieMoon(event:MouseEvent):void
{
addChild(movieMoon);
}

我不建议这样做,因为它会使事情变得不必要的复杂。现在,对于单个按钮,您拥有 3 倍的影片剪辑/精灵、添加/删除子事件处理程序以及要检查/调试的其他变量。将其乘以您拥有的按钮数量。

相反,我建议使用或扩展 SimpleButton{} 类或编写自己的类来封装行为。

简单按钮类:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/SimpleButton.html

我认为你应该将你的三个月亮状态封装到一个单独的MovieClip中,该电影剪辑将控制所有月相的变化。如果已经是这样,很好。此类 MovieClip-Button 类型对象的一般原则是将侦听器分配给父实例,而不是分配给该按钮的各个部分。

public class Moon extends Sprite {
    private var upState:swayingMoon=new swayingMoon();
    private var overState:glowMoon=new glowMoon();
    private var downState:clickedMoon=new clickedMoon();
    private var areWeClicked:Boolean;
    private var areWeOver:Boolean;
    public function Moon() {
        areWeClicked=false;
        areWeOver=false;
        addChild(upState);
        addEventListener(MouseEvent.ROLL_OVER, showHoverMoon);
        addEventListener(MouseEvent.ROLL_OUT, hideHoverMoon);
        addEventListener(MouseEvent.CLICK, showClickedMoon);
        addEventListener(Event.COMPLETE, hideClickedMoon);
    }
    private function showHoverMoon(e:MouseEvent):void {
        areWeOver=true;
        if (areWeClicked) return;
        removeChild(upState);
        addChild(overState);
    }
    private function hideHoverMoon(e:MouseEvent):void {
        areWeOver=false;
        if (areWeClicked) return;
        removeChild(overState);
        addChild(upState);
    }
    private function showClickedMoon(e:MouseEvent):void {
        if (areWeClicked) {
            downState.gotoAndPlay(1);
            return;
        }
        if (overState.parent) removeChild(overState); else removeChild(upState);
        addChild(downState);
        downState.gotoAndPlay(1); // your clicked moon seems to be a playing MC, so starting it over
        areWeClicked=true;
    }
    private function hideClickedMoon(e:Event):void {
        if (e.target!=downState) return; // not our event
        if (!areWeClicked) return;
        areWeClicked=false;
        removeChild(downState);
        if (areWeOver) addChild(overState); else addChild(upState);
    }
}

现在,您的父类正在控制发生的情况和时间。我假设您的clickedMoon MC 只会完整播放一次,然后将事件Event.COMPLETE调度给自己,以便其父级收到通知并采取行动。

最初,你的事件侦听器结构阻止你隐藏mainMoon MC,否则你的其他侦听器永远不会采取行动,现在你正在使用父对象来监听事件,并且可以安全地删除月球的一部分。

Just for a custom button you are going too much complex way. one movieClip is enough for create the button. First create a movieClip and inside that movieClip's timeline create two more frame for 'hover' and click effect. here is little bit of code to start.

var myButton_btn:CustomButton = new CustomButton();
addChild(myButton_btn);
myButton_btn.x = 100;
myButton_btn.y = 100;
myButton_btn.addEventListener(MouseEvent.ROLL_OVER, onOver);
myButton_btn.addEventListener(MouseEvent.ROLL_OUT, onOut);
myButton_btn.addEventListener(MouseEvent.CLICK, onClick);
function onOver(event:MouseEvent):void {
    //trace('over');
    event.target.gotoAndStop('hover');
}
function onOut(event:MouseEvent):void {
    //trace('normal');
    event.target.gotoAndStop('normal');
}
function onClick(event:MouseEvent):void {
    //trace('click');
    event.target.gotoAndStop('click');
}

相关内容

  • 没有找到相关文章

最新更新