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 youtonCompleteHandler