2

我有三个电影剪辑都链接到舞台,我希望它们表现得像一个按钮/但我没有使用按钮,因为我还没有找到一种方法让每个部分(上、下、下、击)都被动画和不仅仅是在使用鼠标时更改。到目前为止,我已经能够让所有三个都出现在我的舞台上,并在我将鼠标悬停以及单击时显示出来,但我认为我在 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);
}
4

3 回答 3

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

现在,您的父类正在控制发生的事情和时间。我假设您的clickedMoonMC 只会完全播放一次,然后Event.COMPLETE向其自身发送一个事件,以便其父级收到通知并采取行动。

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

于 2012-12-10T09:24:08.807 回答
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');

}
于 2012-12-10T10:10:40.417 回答
0

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

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

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

于 2012-12-10T09:16:22.333 回答