具有多个按钮的AS3 addChild顶层失败

时间:2015-07-30 05:02:18

标签: actionscript-3 removechild addchild

这似乎是一个奇怪的,因为它看起来非常简单,我有一个movieclip,其中包含一个后退和前进按钮,用于调用两组按钮的补间,以便在屏幕上滑动addChild然后创建补间然后removeChild for旧的按钮组将新的按钮留在屏幕上。

我的问题是当第二组进入所有按钮时,影片剪辑顶层的按钮无法正常工作(rollOver Tween和all)如果我更改顶层的哪个按钮然后那个按钮失败

关于为什么会出现这种情况的任何想法,以便我可以解决问题?

流程代码如下。

import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.Strong;
import flash.events.*;
import flash.display.MovieClip;

fOverlay.addEventListener(MouseEvent.ROLL_OVER, mOver);
fOverlay.addEventListener(MouseEvent.ROLL_OUT, mOut);
fOverlay.addEventListener(MouseEvent.CLICK, mDown);
bOverlay.addEventListener(MouseEvent.ROLL_OVER, mOverB);
bOverlay.addEventListener(MouseEvent.ROLL_OUT, mOutB);
bOverlay.addEventListener(MouseEvent.CLICK, mDownB);
var butts1:MovieClip = new sButtons1;
var butts2:MovieClip = new sButtons2;

addChild(butts1);
butts1.x = -10;
butts1.y = 0;

function mOver(e:MouseEvent)
{
    var mouseOTween:Tween = new Tween(fBack, "alpha", Strong.easeOut,0.2,0.8,1,true);
}

function mOut(e:MouseEvent)
{
    var mouseOTween:Tween = new Tween(fBack, "alpha", Strong.easeOut,0.8,0.2,1,true);
}

function mOverB(e:MouseEvent)
{
    var mouseOTween:Tween = new Tween(bBack, "alpha", Strong.easeOut,0.2,0.8,1,true);
}

function mOutB(e:MouseEvent)
{
    var mouseOTween:Tween = new Tween(bBack, "alpha", Strong.easeOut,0.8,0.2,1,true);
}

function mDown(e:MouseEvent)
{
    if (butts1.stage)
{
    addChild(butts2)
    butts2.x = 1832;
    butts2.y = 0;

    var nTweenout1:Tween = new Tween(butts1,"x", Strong.easeInOut, -10, -1860, 1, true);
    var nTweenin1:Tween = new Tween(butts2,"x", Strong.easeInOut, 1832, -10, 1, true);
    nTweenout1.addEventListener(TweenEvent.MOTION_FINISH, moFinish1);

    function moFinish1(e:TweenEvent)
    {
        removeChild(butts1);
    }
}

else if (butts2.stage)
{
    addChild(butts1)
    butts1.x = 1832;
    butts1.y = 0;

    var nTweenout2:Tween = new Tween(butts2,"x", Strong.easeInOut, -10, -1860, 1, true);
    var nTweenin2:Tween = new Tween(butts1,"x", Strong.easeInOut, 1832, -10, 1, true);
    nTweenout2.addEventListener(TweenEvent.MOTION_FINISH, moFinish2);

    function moFinish2(e:TweenEvent)
    {
        removeChild(butts2);
    }
}

else
{
    MovieClip(root).addChild(butts1)
    butts1.x = -10;
    butts1.y = 0;
}
}

function mDownB(e:MouseEvent)
{
    if (butts1.stage)
    {
        addChild(butts2)
        butts2.x = -1860;
    butts2.y = 0;

    var nTweenoutB1:Tween = new Tween(butts2,"x", Strong.easeInOut, -1860, -10, 1, true);
    var nTweeninB1:Tween = new Tween(butts1,"x", Strong.easeInOut, -10, 1832, 1, true);
    nTweenoutB1.addEventListener(TweenEvent.MOTION_FINISH, moFinish3);

    function moFinish3(e:TweenEvent)
    {
        removeChild(butts1);
    }
}

else if (butts2.stage)
{
    addChild(butts1)
    butts1.x = -1860;
    butts1.y = 0;

    var nTweenoutB2:Tween = new Tween(butts1,"x", Strong.easeInOut, -1860, -10, 1, true);
    var nTweeninB2:Tween = new Tween(butts2,"x", Strong.easeInOut, -10, 1832, 1, true);
    nTweenoutB2.addEventListener(TweenEvent.MOTION_FINISH, moFinish4);

    function moFinish4(e:TweenEvent)
    {
        removeChild(butts2);
    }
}

else
{
    addChild(butts1)
    butts1.x = -10;
    butts1.y = 0;
}
}

1 个答案:

答案 0 :(得分:0)

我怀疑你的物体彼此重叠。在这种情况下,只有最顶层的对象才会收到鼠标事件。如果是这样,那就是预期的行为。用透明的png可以看到这方面的一个例子。

overlap example

如果没有您的DisplayList结构的其余部分或用户界面的外观,我无法确切地说出您的事件会受到什么影响。但是,我认为下面的代码可能有助于澄清一些事情。

import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.Strong;
import flash.events.*;
import flash.display.MovieClip;
var butts1:MovieClip = new sButtons1;
var butts2:MovieClip = new sButtons2;
var lookup:Dictionary = new Dictionary(true);
var removeTarget:MovieClip = null; // This is the next object to be removed from onscreen
var onComplete:Tween = null;  // A reference to the tween the calls our remove

function init():void {
    // It's healthy to keep your initialization routine grouped away from the rest of your program logic.
    // Optionally, you can recall the function to rerun first time setup, if necessary.
    addChild(butts1);
    butts1.x = -10;
    butts1.y = 0;

    // Use event target as a key in our dictionary to correspond which objects should be hidden/shown
    // This way, we can keep our code consistent and maintainable from one function.
    lookup[fOverlay] = {
        "back":fBack,
        "out":butts1,
        "in":butts2
    }

    lookup[bOverlay] = {
        "back":bBack,
        "out":butts2,
        "in":butts1
    }

    // Because we're registering the same events to the same functions, we can handle it through a loop.
    var overlayEvents:Array = ["rollOver", "rollOut", "click"];
    for (var overlay in lookup) {
        for each (var type:String in overlayEvents) {
            overlay.addEventListener(type, overlayListener, false, 0, true);
        }
    }
}


function overlayListener(e:MouseEvent):void {
    // Having generalized our objects, we swap for the appropriate object based on the type of event.
    switch (e.type) {
        case "rollOver":
            new Tween(lookup[e.currentTarget].back, "alpha", Strong.easeOut,0.2,0.8,1,true);
            break;
        case "rollOut":
            new Tween(lookup[e.currentTarget].back, "alpha", Strong.easeOut,0.8,0.2,1,true);
            break;
        case "click":
            var show:MovieClip, hide:MovieClip;

            if (lookup[e.currentTarget].out.stage) {
                hide = lookup[e.currentTarget].out;
                show = lookup[e.currentTarget].in;
            } else if (lookup[e.currentTarget].in.stage) {
                hide = lookup[e.currentTarget].in;
                show = lookup[e.currentTarget].out;
            } else {
                show = butts1;
                addChild(show);
            }

            show.x = 1832;
            show.y = 0;

            new Tween(hide, "x", Strong.easeInOut, -10, -1860, 1, true);
            if (hide != null) {
                removeTarget = hide;
                if (onComplete != null) {
                    onComplete.stop()
                    onComplete.rewind()
                }

                onComplete = new Tween(show, "x", Strong.easeInOut, 1832, -10, 1, true);
                onComplete.addEventListener("motionFinish", remove, false, 0, true);
            }
            break;
    }
}

function remove(e:Event):void {
    if (removeTarget != null) {
        removeChild(removeTarget)
        removeTarget = null;
        onComplete.removeEventListener("motionFinish", remove);
        onComplete = null;
    }
}

init();