ActionScripting问题:添加/删除子项

时间:2012-12-10 04:56:24

标签: actionscript-3 actionscript event-handling removechild addchild

我有三个影片剪辑都链接到舞台,我希望它们像按钮一样/但我没有使用按钮,因为我没有找到一种方法来获得每个部分(向上,向上,向下,击中)动画,而不只是在鼠标使用时更改。到目前为止,我已经能够让所有三个出现在我的舞台上,当我点击鼠标时显示,但是当我点击时,我认为我正在做一些错误的removeChild。每个MC应该一次出现一个,现在当我悬停在所有三个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 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

我认为你应该将你的三个月亮状态封装到一个单独的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,否则您的其他侦听器将永远无法操作,现在您正在使用父对象来侦听事件,并且可以安全地删除部分月球。

答案 2 :(得分:0)

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');

}