深处的切换导致MC的重复深处、MC

2023-09-08 13:10:16 作者:徊卟菿の濄呿

这是一个previous问题更多信息这一次重新发布。

中的文件: 2shared.com/file/hRKhEiqh/Script_Test.html 和< A HREF =htt​​p://2shared.com/video/UZNmqzXt/a_anconeus.html相对=nofollow> 2shared.com/video/UZNmqzXt/a_anconeus.html

这个问题是可重现我的机器上使用ActionScript 3.0中新的.fla文件的项目在Flash Professional CS5。这是我原来的问题提供更多信息的编辑。

我工作的一个项目,加载外部SWF的和搜索实例名称匹配的关键词,即拖和降,以确定影片剪辑的比赛,然后把事件侦听器,这些MC的包含对D和D事件听众和code。

具体的问题是深度的影片剪辑嵌套在动态加载外部SWF文件的切换。

在哪里我有麻烦的是具体的命令:

swapChildrenAt,setChildIndex,swapChildren,removeChild之/的addChild。我已经尝试了所有四连重复了同样的问题。让我来解释一下。

我的世界 MC小课堂开课了,羊随意改变颜色 只需要一样东西

在一个可拖动的MC被点击时,它被转移到动态加载SWF的顶部指数因此它上面的SWF一切可见。问题是,任何试图这些命令的所有复制MC。什么情况是这样的:

在MC MOUSE_DOWN事件触发: 目标的MC索引被记录为2,该索引我们将切换到是20(在SWF的最大索引) setChildIndex被称为目标MC父:mc.parent.setChildIndex(MC,(mc.parent.numChildren-1)) MC移动到指数20则MC的另一个实例是在它位于previously(指数2),发生这种情况的MOUSE_DOWN活动结束后,我不知道什么时候。

图层创建

此副本已使用连接到舞台EVENT.ADDED_TO_STAGE监听器,将用一个添加到舞台的MovieClip类型的每个对象确认。我检查了SWF,确认没有重复的影片剪辑的痕迹也证实了这一点。

类似的帖子提到同样的重复的问题,但没有人有我发现将工作在我的情况的解决方案。

http://www.kirupa.com/forum/showthread.php? T = 359452 http://board.flashkit.com/board/showthread.php?t=775200 http://forums.adobe.com/thread/199983

随着最后一个音符,如果我注释掉指数掉期的说法,code完美的作品与MC只是停留在其下的z-index而不是被放在上面。不幸的是,这不会是一个解决方案,因为我不是建立外部SWF的工作。

要复制这种行为,你需要一个外部的AS3 SWF两个至少有两个MC的实例名drag01和drop01或东西相匹配的关键字拖和下降。点字符串变量SWF'到该文件,你会看到重复的问题。

感谢和问候 卡梅伦

编辑:新的修剪code,复制到空白的AS 3.0文件同样的错误

 进口flash.net.URLRequest;
进口flash.display.Loader;
进口对象类型:flash.events.Event;
进口flash.events.ProgressEvent;
进口的flash.display.MovieClip;
进口flash.display.DisplayObject;
进口flash.events.MouseEvent;
进口API元素flash.text.TextField;
进口flash.text.TextFormat用于;
进口flash.net.URLLoader;




VAR SWF:字符串=a_anconeus.swf;
loadSWF(SWF);

stage.addEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStageReport,真正的);
stage.addEventListener(Event.ADDED_TO_STAGE,onAddedToStageReport,真正的);

功能onRemovedFromStageReport(EVT:事件)
{
    {
        TRACE(去掉+ evt.target.name +在深度:+ evt.target.parent.getChildIndex(evt.target));
    }
}


功能onAddedToStageReport(EVT:事件)
{
    {
        跟踪(又说:+ evt.target.name +在深度:+ evt.target.parent.getChildIndex(evt.target));
    }
}

功能onDragMouseDown(事件:MouseEvent)方法:无效
{
    VAR阻力:影片剪辑=影片剪辑(将event.target);
    VAR topPosition:UINT = drag.parent.numChildren  -  1;
    TRACE(点击+ drag.name +,ONTARGET =+ drag.ontarget +,当前Z+ drag.parent.getChildIndex(拖)+新的z:+ topPosition);
    //drag.parent.setChildIndex(drag,topPosition);
    VAR indexToDelete:INT = drag.parent.getChildIndex(阻力);

//this.parent.removeChild(this);
        跟踪(索引中删除:+ indexToDelete +,那里的东西:+ drag.parent.getChildAt(indexToDelete)。名称);
        drag.startDrag();
        //drag.parent.setChildIndex(drag,drag.parent.numChildren-1); //设置儿童深度顶部
        跟踪(改变后,那里的东西:+ drag.parent.getChildAt(indexToDelete)。名称);
        //drag.parent.swapChildren(drag,drag.parent.getChildAt(drag.parent.numChildren -1));


}

功能onDragMouseUp(事件:MouseEvent)方法:无效
{
    跟踪(老鼠了起来:+ event.target.name +,索引:+ event.target.parent.getChildIndex(将event.target));
    VAR阻力:影片剪辑=影片剪辑(将event.target);

    drag.stopDrag(); //影片剪辑有简单的拖放方法

}

/ *功能dragEnterFrameHandler(事件:事件):无效{

    VAR阻力:影片剪辑=影片剪辑(将event.target);

    如果(drag.mousedown ==假)
    {

        如果(drag.onTarget ==真)
        {
            //将其发送至所述液滴的X / Y
            drag.x  -  =(drag.x  -  drag.dropon.x)/ 5;
            drag.y  -  =(drag.y  -  drag.dropon.y)/ 5;
        }

        否则,如果(drag.onTarget ==假)
        {
            drag.x  -  =(drag.x  -  drag.homeX)/ 5;
            drag.y  -  =(drag.y  -  drag.homeY)/ 5;
        }
    }
} * /



功能loadSWF(文件路径:字符串)
{

    跟踪(调用装载机);
    VAR装载机:装载机=新的Loader();
    变种网址:字符串=文件路径;
    loader.contentLoaderInfo.addEventListener(引发Event.COMPLETE,onCompleteHandler);

    loader.load(新的URLRequest(URL));

}





功能onAddedToStage(EVT:事件)
{
    跟踪(调用onAddedToStage);
    / *如果(evt.target为影片剪辑)//过滤器仅影片剪辑
    {
        变种MC:影片剪辑=影片剪辑(evt.target);
       //迹线(onAddedToStage,EVT:+ mc.name +,+(mc.parent.numChildren-1));

        如果(字符串(mc.name).indexOf(拖)!= -1)
        {
            。VAR dropString:字符串=字符串(mc.name).split(拖)加入(水滴);

            如果(mc.parent.getChildByName(dropString))
            {
                跟踪(我们的DropTarget:+ mc.dropon);
                mc.addEventListener(的MouseEvent.MOUSE_DOWN,onDragMouseDown);
                mc.addEventListener(侦听MouseEvent.MOUSE_UP,onDragMouseUp);
                mc.buttonMode =真;

            }
        }
    } * /
}

功能onCompleteHandler(比如LoadEvent:事件)
{
    跟踪(加载完成);
    VAR SWF:影片剪辑=影片剪辑(loadEvent.currentTarget.content);
    //swf.addEventListener(Event.ADDED_TO_STAGE,onAddedToStage,真正的);
    //跟踪(事件连接,添加子);

跟踪(子项添加启动);
    的addChild(SWF);

    跟踪(孩子增加成品);
    VAR孩子:数=(swf.numChildren-1);
    跟踪(孩子们:+孩子);
    对于(VAR我:数量= 0; I&LT; =儿童;我++)
    {
        跟踪(LOOP从这里开始);
        如果(swf.getChildAt(i)是影片剪辑)
        {
            //跟踪(MC:是的);
            变种MC:影片剪辑=影片剪辑(swf.getChildAt(一));
            跟踪(姓名:+ mc.name);

            如果(字符串(mc.name).indexOf(拖)!= -1)
            {
                。VAR dropString:字符串=字符串(mc.name).split(拖)加入(水滴);

                如果(swf.getChildByName(dropString))
                {
                    跟踪(删除+ mc.name);
                    children--;
                    swf.removeChild(MC);
                    跟踪(删除);
                    //swf.addChild(mc);


                    /*mc.addEventListener(MouseEvent.MOUSE_DOWN,onDragMouseDown);
                    mc.addEventListener(侦听MouseEvent.MOUSE_UP,onDragMouseUp);
                    mc.buttonMode = TRUE; * /

                }
            }
        }
    }
跟踪(完成功能);
}
 

解决方案

解开了谜底:)

看着你的SWF,我可以看到它有两帧,所以一旦你开始拖动,下一帧渲染和对象回来。

要测试这个我加了 swf.stop(); 来YOUT onCompleteHandler

this is a repost of a previous question with more info this time.

The files: 2shared.com/file/hRKhEiqh/Script_Test.html and 2shared.com/video/UZNmqzXt/a_anconeus.html

This issue is reproducible on my machine with a new .fla project in Actionscript 3.0 in Flash Professional CS5. It's an edit of my original question with more information.

I'm working on a project to load external SWF's and search through instance names for matching keywords, namely 'drag' and 'drop' to identify movieclip matches, then attach event listeners to these MC's which contain the D&D event listeners and code.

The specific problem is the switching of depths for Movieclips nested in dynamically loaded external SWF files.

Where I am having trouble is the specific commands:

swapChildrenAt, setChildIndex, swapChildren, removeChild/addChild. I've tried all four with the same problem of duplication. Let me explain.

When a draggable MC is clicked, it is moved to the top index of the dynamically loaded SWF so it's visible above everything else in that SWF. The problem is that trying any of these commands all duplicate the MC. What happens is this:

MOUSE_DOWN event fires on MC: Index of target MC is recorded as '2', the index we will switch to is '20' (maximum index of the SWF) setChildIndex is called on the target MC parent: mc.parent.setChildIndex(mc, (mc.parent.numChildren-1)) MC moves to index 20 then another instance of the MC is created at the layer it was located previously (index 2), this happens after the MOUSE_DOWN event finishes, I'm not sure exactly when.

This duplicate has been confirmed using the EVENT.ADDED_TO_STAGE listener attached to the stage to catch every object of MovieClip types that is added to the stage. I've inspected the SWF to confirm there's no duplicate MovieClips, the traces also confirm this.

Similar posts mention the same duplication problem but no one has a solution that I've found will work in my case.

http://www.kirupa.com/forum/showthread.php?t=359452 http://board.flashkit.com/board/showthread.php?t=775200 http://forums.adobe.com/thread/199983

As a last note, if I comment out the index swap statement, the code works perfectly with the MC simply staying on its lower z-index instead of being on top. Unfortunately that's not going to work as a solution since I'm not building the external SWF's.

To duplicate this behavior, you need an external AS3 SWF with two at least two MC's with instance names "drag01" and "drop01" or something that matches the keywords 'drag' and 'drop'. Point the String variable 'SWF' to that file and you should see the duplication problem.

Thanks and regards Cameron

Edit: new trimmed code, copied into a blank AS 3.0 file with the same errors.

    import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.net.URLLoader;




var swf:String = "a_anconeus.swf";
loadSWF(swf);

stage.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStageReport, true);
stage.addEventListener ( Event.ADDED_TO_STAGE , onAddedToStageReport , true ) ;

function onRemovedFromStageReport (evt:Event)
{
    {
        trace("REMOVED: " + evt.target.name + " at depth: " + evt.target.parent.getChildIndex(evt.target));
    }
}


function onAddedToStageReport (evt:Event)
{
    {
        trace("ADDED: " + evt.target.name + " at depth: " + evt.target.parent.getChildIndex(evt.target));
    }
}

function onDragMouseDown(event:MouseEvent):void
{
    var drag:MovieClip = MovieClip(event.target);
    var topPosition:uint = drag.parent.numChildren - 1;
    trace("click: "+drag.name +", ontarget = "+drag.ontarget + ", current z: " + drag.parent.getChildIndex(drag) + " new z: " + topPosition);
    //drag.parent.setChildIndex(drag, topPosition);
    var indexToDelete:int = drag.parent.getChildIndex(drag);

//this.parent.removeChild(this);
        trace("index to delete: " + indexToDelete +", what's there: " + drag.parent.getChildAt(indexToDelete).name);
        drag.startDrag();
        //drag.parent.setChildIndex(drag, drag.parent.numChildren-1); //set child depth to top
        trace("after change, what's there: " + drag.parent.getChildAt(indexToDelete).name);
        //drag.parent.swapChildren(drag, drag.parent.getChildAt(drag.parent.numChildren -1));


}

function onDragMouseUp(event:MouseEvent):void
{
    trace("mouse up: "+event.target.name + ", index: " +event.target.parent.getChildIndex(event.target));
    var drag:MovieClip = MovieClip(event.target);

    drag.stopDrag(); //Movieclips have simple drag methods

}

/*function dragEnterFrameHandler(event:Event):void {

    var drag:MovieClip = MovieClip(event.target);

    if (drag.mousedown == false)
    {

        if (drag.onTarget == true)
        {
            //send it to the drop X/Y
            drag.x -= (drag.x - drag.dropon.x)/5;
            drag.y -= (drag.y - drag.dropon.y)/5;
        }

        else if (drag.onTarget == false)
        {
            drag.x -= (drag.x - drag.homeX)/5;
            drag.y -= (drag.y - drag.homeY)/5;
        }
    }
}*/



function loadSWF(filepath:String)
{

    trace("calling loader");
    var loader:Loader = new Loader();
    var url:String = filepath;
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);

    loader.load(new URLRequest(url));

}





function onAddedToStage ( evt:Event )
{
    trace("calling onAddedToStage");
    /*if (evt.target is MovieClip) //filter only movieclips
    {
        var mc:MovieClip = MovieClip(evt.target);
       // trace ( "onAddedToStage, evt: " + mc.name + ", " +(mc.parent.numChildren-1) ) ;

        if (String(mc.name).indexOf("drag") != -1) 
        {
            var dropString:String = String(mc.name).split("drag").join("drop");

            if (mc.parent.getChildByName(dropString))
            {
                trace("our droptarget: " + mc.dropon);
                mc.addEventListener(MouseEvent.MOUSE_DOWN, onDragMouseDown);
                mc.addEventListener(MouseEvent.MOUSE_UP, onDragMouseUp);
                mc.buttonMode = true;

            }
        }
    }*/
}

function onCompleteHandler(loadEvent:Event)
{
    trace("load complete");
    var swf:MovieClip = MovieClip(loadEvent.currentTarget.content);
    //swf.addEventListener ( Event.ADDED_TO_STAGE , onAddedToStage, true ) ;
    //trace("event attached, adding child");

trace("child add started");
    addChild(swf);

    trace("child add finished");
    var children:Number = (swf.numChildren-1);
    trace("children: " + children);
    for (var i:Number = 0; i <= children; i++)
    {
        trace("LOOP STARTS HERE");
        if (swf.getChildAt(i) is MovieClip)
        {
            //trace("MC: yes");
            var mc:MovieClip = MovieClip(swf.getChildAt(i));
            trace("name: " + mc.name);

            if (String(mc.name).indexOf("drag") != -1) 
            {
                var dropString:String = String(mc.name).split("drag").join("drop");

                if (swf.getChildByName(dropString))
                {
                    trace("removing: " + mc.name);
                    children--;
                    swf.removeChild(mc);
                    trace("removed");
                    //swf.addChild(mc);


                    /*mc.addEventListener(MouseEvent.MOUSE_DOWN, onDragMouseDown);
                    mc.addEventListener(MouseEvent.MOUSE_UP, onDragMouseUp);
                    mc.buttonMode = true;*/

                }
            }
        }
    }
trace("finish function");
}

解决方案

mystery solved :)

looking at your swf I can see that it has two frames, so as soon as you start dragging, the next frame renders and the object comes back.

to test this I added swf.stop(); to yout onCompleteHandler

 
精彩推荐
图片推荐