PDA

View Full Version : duplicateMovieClip() effect in as3.0


springframework
02-02-2007, 08:04 PM
Does anyone know a way of duplicating a Sprite object.

senocular
02-02-2007, 08:13 PM
That feature has been removed. I've taken the best you can do and wrapped that in a function you can find here:
http://www.senocular.com/flash/actionscript.php?file=ActionScript_3.0/com/senocular/display/duplicateDisplayObject.as

Note: this currently compensates for a bug that may be fixed in a later release (or might event be fixed now in 9.0.28.0)

springframework
02-02-2007, 11:25 PM
im confused on what you say in your documentation.

"If using Flash 9, make sure you export for ActionScript the symbol you are duplicating"




im trying to copy a Loader object, image_loader, which has already loaded an image


var copy_image:Loader = new Loader();
copy_image = Loader(duplicateDisplayObject(image_loader));
addChild(copy_image);

springframework
02-02-2007, 11:47 PM
what are these two lines of code doing...


var targetClass:Class = (target as Object).constructor;
var duplicate:DisplayObject = new targetClass() as DisplayObject;

SecretAgentRege
02-13-2007, 05:33 PM
Hey Senocular,

I'm using your duplicateDO, but I'm running into some small trouble when trying to duplicate a Sprite:

Implicit coercion of a value with static type flash.display: DisplayObject to a possibly unrelated type flash.display:Sprite.


I can get around this by changing the return type to a Sprite, and the duplicate instance type to a Sprite and the new targetClass as Sprite.

Is there something else I'm doing wrong? Obviously, I would not want to limit myself to just Sprites. Just wondering if perhaps something isn't working like it should, as your example usage is duplicating a Sprite...?

I'm using FlashDevelop with the Flex2 mxmlc.exe if that was to help at all?

Thanks!

dr_zeus
02-13-2007, 07:18 PM
Is there something else I'm doing wrong?

Try casting the result to Sprite.

var myCopiedSprite:Sprite = duplicateDisplayObject(mySprite) as Sprite;

SecretAgentRege
02-13-2007, 08:16 PM
Yup, that ended up working. Thanks.

I'm wondering why I have to do that if it wasn't written like that (and obviously then worked without it)?

dr_zeus
02-14-2007, 05:46 PM
Sprite is a subclass of DisplayObject. The function was designed to duplicate any type of DisplayObject. The function can only assume that it returns a DisplayObject, and you need to be smart and tell it what you actually sent in as a parameter (Sprite in your case) by casting the returned value.

springframework
02-14-2007, 08:22 PM
Does DuplicateDisplayObject also duplicate all the children of the DisplayObject you are trying to duplicate?

bfunc
11-22-2009, 09:57 PM
Hi a question may be stupid but i can't solve it for a long time..
i am trying to duplicate dynamicly loaded sprite with image

import ...
import com.senocular.display.duplicateDisplayObject;

var db:Sprite=new Sprite();// = new bb(); // import from library - duplicating works perfect

readXML("filelist.xml");

function readXML(XMLpath:String):void {
var loader:URLLoader=new URLLoader(new URLRequest(XMLpath));
loader.addEventListener(Event.COMPLETE, xmlDone);
}
function xmlDone(e:Event):void {
var xml:XML=XML(e.target.data);
var path=xml.item[1];
var im1:imageContainer=new imageContainer(110,110,path,"loader.swf"); // returns displayObject
db.addChild(im1);
addChild(db);

var newInstance:Sprite = duplicateDisplayObject(db,true) as Sprite; // doesn't works!!!
addChild(newInstance);
newInstance.x=200;

}