Hey there,
this is my first time turning to this forum for help. I’m being baffled by something, and I’m willing to bet there are people on this forum who can help me out. The prize if you do; lots of good karma from me
I’m working on an online flash gaming platform. We load all our graphics in externally from a library. I want to be able to load something in, then duplicate it for reuse.
So, I came across senoculars example code for duplicating DisplayObjects. This is where im getting problems. Some of the loaded assest have a few bits of timeline code (basically stop() on animated parts which have multiple animations. also, there are locators in there which are used for the collision detection engine, and these have this.visible=false commands in them).
When i load in a graphic asset, everything works fine- all timeline code executes, the loaded in swf behaves exactly how i want it to. When i duplicate it, buried timeline code is being ignored. Also, even more bafflingly- until i put some kind of timeline code on the top layer of the movieclip that is being imported and duplicated, the duplicated display object wouldn’t display at all when added to the stage. (just putting a stop or trace at the very top layer made it work)
So i do something like this to load a swf in, and duplicate it
var myDoAfter = function(mcLoader:Loader)
{
loadedGraphic = mcLoader.content
var newClone = duplicateDisplayObject(loadedGraphic)
addChild(newClone)
}
myLoader(graphicURL, myDoAfter)
myLoader is a function written by a colleague thats part of the sites main library. If your interested, it looks a little like this
function myLoader(passed_url, passed_doAfter)
{
var request:URLRequest= new URLRequest(passed_url)
var mcLoader:Loader = new Loader();
mcLoader.load(request)
// handles some load progress stuff here
// when the item has been fully loaded
passed_doAfter(mcLoader)
}
and the duplicate code looks something like this
public static function duplicate(target):DisplayObject
{
var targetClass:Class = Object(target).constructor;
var duplicate = new targetClass();
duplicate.transform = target.transform;
duplicate.filters = target.filters;
duplicate.cacheAsBitmap = target.cacheAsBitmap;
duplicate.opaqueBackground = target.opaqueBackground;
return duplicate;
}
I’ve waffled already, and have tried to keep the code brief. Im sure some people are going to say ‘never put code on the timeline in AS3’ which is probably legitimate (its not all my doing).
Still, i am intreagued to suss it out. Is it that, using this method of duplicating (ie getting the constructor of the thing im trying to duplicate), timeline code is simply going to be ignored? If so, why can’t i duplicate something unless it has some timeline code at the top level? Even when it doesnt, the initial load of the asset works correctly- i can add the original to the stage, but its clones seem to be empty.
Might it be something to do with the casting of the thing loaded in? (ie that Loader.content is going to be a displayObject rather than a MovieClip) ?
I am hardly expecting a complete answer, but if anyone can give me any useful hints id be extremely grateful