I am writing a flash application that just uses Actionscript code. The application uses information from a database to allow a user to select items that are displayed to them on-screen by loading a SWF file dynamically. All of this is working fine. When An SWF is loaded if a user clicks on the image it needs to be duplicated and the duplicate can be dragged onto another area of the screen. It is here that I am hitting a brick wall.
I have tried using the duplicateDisplayObject() code from senocular with no success. It appears to duplicate something because I can see in the debugger that the numChildren value of the Sprite to which I am adding the duplicate increases, but nothing is displayed on the screen. Is this because it is a dynamically loaded movieClip? Do I need to be adding to the library in some way so that it can be instantiated? If I do how would I do that since I can’t find any reference to it in any of the books I have or searching I have done?
I have created a code snippet to show the problem:
package {
import flash.events.;
import flash.net.;
import flash.display.*;
import flash.geom.Rectangle;
public class test extends Sprite {
public var currentAssembly = new Sprite;
public var currentAssemblyLoader:Loader = new Loader();
public var currentAssemblyURL:URLRequest = new URLRequest();
public function test():void {
currentAssemblyLoader.contentLoaderInfo.addEventListener(Event.INIT, assemblyLoaded, false, 0, true);
currentAssemblyLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress, false, 0, true);
currentAssemblyLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
currentAssemblyLoader.load(new URLRequest("http://panelec.twoslackers.com/graphics/CBAT1L1.swf") );
}
public function assemblyLoaded(evt:Event):void {
var theSWF:DisplayObject = evt.target.content;
currentAssemblyLoader.unload();
currentAssembly.addChild(theSWF);
currentAssembly.x = 10;
currentAssembly.y = 10;
currentAssembly.scaleX = .5;
currentAssembly.scaleY = .5;
currentAssembly.addEventListener(MouseEvent.CLICK, currentAssemblyMouseClick, false, 0 , true);
addChild(currentAssembly);
trace("loaded");
}
function currentAssemblyMouseClick(evt:Event):void {
var copy:DisplayObject = duplicateDisplayObject(currentAssembly);
copy.x = 50;
copy.y = 50;
addChild(copy);
trace("click");
}
public function duplicateDisplayObject(target:DisplayObject):DisplayObject {
// create duplicate
var targetClass:Class = Object(target).constructor;
var duplicate:DisplayObject = new targetClass();
// duplicate properties
duplicate.transform = target.transform;
duplicate.filters = target.filters;
duplicate.cacheAsBitmap = target.cacheAsBitmap;
duplicate.opaqueBackground = target.opaqueBackground;
if (target.scale9Grid) {
var rect:Rectangle = target.scale9Grid;
// WAS Flash 9 bug where returned scale9Grid is 20x larger than assigned
// rect.x /= 20, rect.y /= 20, rect.width /= 20, rect.height /= 20;
duplicate.scale9Grid = rect;
}
return duplicate;
}
public function loadProgress(evt:ProgressEvent):void {
trace("loading");
}
public function onIOError(evt:IOErrorEvent):void {
trace("Unable to load data.");
}
}
}
The duplicateDisplayObject function has been edited to remove the auto-add feature. If you place breakpoints at the trace statements in the assemblyLoaded and currentAssemblyMouseClick functions you will see that this.numChildren is 1 at the first breakpooint and increases by 1 for each click on the graphic. Something is being added but nothing new is being displayed.
I have been hacking at this for two days now and am pretty frustrated as this can’t be something that is unusual to do. What am I missing?
Is it relevant hat the moviclips I am loading are vector graphics created by importing an Adobe Illustrator file into Flash and then exporting the file to create an SWF?
A supplementary question is about where the object actually is? Let’s say that I load an SWF and add it (using addChild) to a movieClip called firstMC. Lets’ say that I have a second movieClip called secondMC that I want to move the loaded SWF to. How do I do this in such a way that I can unload the variables used to originally load the movie clip (so that I can load another using the same functions) without the movieclip disappearing from view? Everything I have tried in this respect has been fruitless.