View Full Version : How do I use external loaded swf again?
eudora
May 2nd, 2007, 05:34 AM
I have use the Loader class to load the external swf and they are displaying, but now I want to create a DisplayObject and I want it to contain that same external swf. What is the correct procedure in doing this?
mathew.er
May 2nd, 2007, 06:49 AM
Use the Loader again ;)
If you need it as a DisplayObject, cast it to that type.
Dazzer
May 2nd, 2007, 07:37 AM
There are some people pushing for a true "clone()" method though.
I'm not sure if you can just cast a Loader as a displayobject.
But if you don't want to go into really crappy stuff, just Load it again
mathew.er
May 2nd, 2007, 07:50 AM
Loader inherits from DisplayObject, so e.g. var o : DisplayObject = DisplayObject ( loaderInstance ) shouldn't make any trouble.
eudora
May 2nd, 2007, 07:50 AM
Hi guys,
ok I think I need to explain a bit more on my project. I will have a manager class which loads all assets and swfs. Afterwhich, my game code should be able to getChildByName on the specific type of asset to reuse.
My idea would be along this line, please correct me if i am wrong...
So for example if I am doing a tetris game, my idea would be to load say a graphic for the blocks, blocks_bg.swf. That is just an art asset. From there, whenever my code is pushing new blocks out, I will make a new Block class which contains a Sprite background and other variables. I will then addChild(block_bg) to that Sprite and addChild the entire Block into the display list.
Is my understanding above correct?
Dazzer
May 2nd, 2007, 07:55 AM
yes.
I'm currently building such a system.
And there is an alternative. Its a little confusing. You have to get the class constructor of the asset that you're loading (basically, everytime you compile your swf, it is associated with a class), then call the class constructor.
Another alternative is to use a bitmapData object to draw the asset. But this turns it into a raster object, and it does not scale up anymore.
Unfortunately you cannot just "addChild". Why? Because a displayObject can only have 1 parent. It cannot appear more than once. Therein lies the problem.
eudora
May 2nd, 2007, 09:55 AM
Is it if I addChild(something) that something instantly pops into the new parent and leaves its old parent?
Hmm is there any pointers in CS3?
senocular
May 2nd, 2007, 10:09 AM
Is it if I addChild(something) that something instantly pops into the new parent and leaves its old parent?
Hmm is there any pointers in CS3?
Yes and no.
yes addChild removes from the old parent, no there are no pointers
eudora
May 2nd, 2007, 11:42 AM
If so, how can I add the background.swf to another movieclip using AS? Do I have to reload using the Loader class again?
Dazzer
May 2nd, 2007, 11:45 AM
yes.
well unless you're absolutely sure that you won't be resizing it or scaling it. You can use a bitmapData.draw() to convert into a bitmap.
eudora
May 2nd, 2007, 11:57 AM
Then in that case I will just restructure my code to store the location too... Since I downloaded it the first time, it should be cached in the system too rite? Assuming location is exactly the same...
Btw I am from Singapore too.. :)
devonair
May 2nd, 2007, 12:19 PM
If I understand the question correctly (and that's a big IF), you can use ApplicationDomain.getDefinition("MyClass") to use classes stored in an external .swf.
So for example, say you have a swf named assetSWF that contains a movieclip of a tetris shape that is set to export and linked with the class name Shape1 - in your main .swf file you can load that .swf, then once it's loaded, make use of that class like this..
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLET E, onAssetsLoad);
l.load(new URLRequest("assetSWF.swf"));
var Shape1:Class; // this will be your Shape1 class once the .swf is loaded
function onAssetsLoad(e:Event):void {
Shape1 = e.target.applicationDomain.getDefinition("Shape1");
addShape();
}
// now that the asset swf is loaded you can make use of your Shape1 class as usual
function addShape():void {
var myShape:MovieClip = new Shape1();
addChild(myShape);
}
if I totally misunderstood the question, I apologize...
Dazzer
May 4th, 2007, 02:05 PM
Still trying to work it out... Senocular's method (using constructor), unfortunately, does not work with loader. At least, I can't get it working.
getDefinition also doesn't work, as it actually requires you to have a Symbol named in the loaded swf (you are hence duplicating the Symbol, not the swf).
What a total pain in the arse.
Why did they take away duplicateMovieClip()! -sobs-
stringy
May 4th, 2007, 06:05 PM
Still trying to work it out... Senocular's method (using constructor), unfortunately, does not work with loader. At least, I can't get it working.
getDefinition also doesn't work, as it actually requires you to have a Symbol named in the loaded swf (you are hence duplicating the Symbol, not the swf).
What a total pain in the arse.
Why did they take away duplicateMovieClip()! -sobs-
Don`t forget duplicateMovieClip() could not duplicate a swf loaded into a movie but only movieclips within the movie loaded and the timeline to which it was(is) copied is limited to the parent of the original clip.
I have just tried senoculars duplicate code and it seems to work pretty well.(Tried with 3 different -albeit fairly simple- files)
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, initListener);
var urlRequest:URLRequest = new URLRequest("your.swf");
loader.load(urlRequest);
function initListener (e:Event):void {
for(var i=0;i<2;i++){
var mc:MovieClip=new MovieClip()
mc.x=150*i
var targetClass:Class = loader.content["constructor"];
var duplicate:DisplayObject = new targetClass();
mc.addChild(duplicate)
addChild(mc)
}
}
Dazzer
May 5th, 2007, 01:46 AM
I still can't get it working
did you have to do something to the loaded swf? Some "export for runtime sharing" or what not
stringy
May 5th, 2007, 02:48 AM
No nothing at all -it just works as is. I stuck the code on the timeline-not tried in a seperate class.
Dazzer
May 5th, 2007, 03:25 AM
Mind sending me source pretty pretty please?
stringy
May 5th, 2007, 04:02 AM
Mind sending me source pretty pretty please?
yeh sure.
Check the prev.swf first to see it working before you corrupt it with flash ;)
Dazzer
May 5th, 2007, 05:55 AM
Okay here's what is different
I was actually trying to literally draw something (not using actionscript) and loading that.
Now if i put just drawing, remove the documentclass, nothing happens
If i include the drawing together with the document class, IDE Crashes on me.
Hence the problem. You can't use the method with things without a document class.
Will investigate further... (is hating adobe more and more now)
stringy
May 5th, 2007, 06:29 AM
Ive just made one to load jpgs & duplicate(I know there are more efficient ways) by loading a swf ,passing the file name to it and then that loading in the jpg file.
if you upload your none duplicating swf,i`ll see if it works with it. :)
Dazzer
May 5th, 2007, 06:36 AM
here
stringy
May 5th, 2007, 07:27 AM
yor crashes.swf caused me to crash too
the other can be duplicated as decscibed above-by loading a swf(with document class) and then loading it into that.(edit-have mailed you files)
Dazzer
May 5th, 2007, 12:35 PM
yeh but I don't want that. I don't want my duplicate to be loading anything! totally defeats the purpose.
I want to literally clone a displayobject. No loaders. Nothing.
Because using loader depend on the cache. And by that logic alone, I don't like it.
stringy
May 5th, 2007, 02:22 PM
I think it will be very useful for certain types of transition :beam: Thanks again to senocular.
Dazzer
May 5th, 2007, 11:41 PM
I think it will be very useful for certain types of transition :beam: Thanks again to senocular.
lol
I am thanking him whenever I post. :) Siggy!
As a side note... I am thinking of this:
Convert the display object to be duplicated into a byteArray.
To duplicate, Load byteArray using Loader.
And return displayobject.
Will try that :)
edit:
GAH! SENOCULAR! HELP ME!!!!!!!!!!!!! I tried your byteArray cloning method, but it doesn't work for me :(. And I'm sure that it should work, but the problem is that my object is the "content" object of the loader object. Doesn't seem to like it very much.
Any ideas?
eudora
May 6th, 2007, 08:51 AM
Hi guys, I am a bit confused... so is there another way this can be done?
Right now my engine is using a storage of URL with the object name.. so when needed I will have to use Loader to load the url to get the displayObject. My assumption is that it is cached if I preloaded the exact same url... but if there is a way to exactly clone the displayObject after preloading, that would be best.. hehe
Dazzer
May 6th, 2007, 09:18 AM
I am working on it. As you realise from my past few posts, this is physically just not possible.
Constructor Method -
This will not work with all swfs. And not even graphics.
Getting the constructor sounds like it is a good idea, but just doesn't work for all. The only occasion this works is when
- your swf is a MovieClip, with a documentClass. In this case, the documentClass is responsible for the drawing of itself. Drawing anything on the stage in the IDE will result in a crash and burn.
Now the problem here is that if you had a swf without a document class, the constructor of your swf is MovieClip. Now you might think this is GREAT this might just work. Unfortunately, if you did this
ActionScript Code:
temp:Class = loadedClip["constructor"]
new temp();
you are infact just calling new MovieClip(). And we know what happens when you do that. NOTHING HAPPENS. A new MovieClip is created, and that's it. That's the problem I had earlier. It worked! But nothing from the original swf is there.
ByteArray method
I've been trying my best to get this working. But in fact, I can't get it working for anything.
Basically, you use byteArray.writeObject() to write a byte copy of any object, then call byteArray.readObject() to get a deep copy of it. The problem here is, I tried it with a simple swf, and when I try to get a byte copy of Loader.content, it gives me a bytearray of length 1. That is obviously not really right.
Trying to copy Loader is even worse. Since using deep copy means your class definition goes down the drain, you can't even get a Loader object out of it. Means, basically, you get crap all.
I'm currently on my 3rd theory. Which is similar to the Dynamic Sound techniques of loading a swf with a sound embedded in it, and loading its bytes into a Loader using loadBytes. Unfortunately, this means it is not a true "copy". You're still going to have to use a Loader. But I don't like the idea of the Loader having to depend on a cache (what happens if the Cache runs out? Or a user has set his Cache limit to 0?) And unfortunately, I still cannot convert Loader.content into a byteArray.
I'm not saying it can be done, but I'm not giving up yet.
edit: I just had a 4th idea I would like to try out, while reading over my post.
Instead of the Constructor method, you can actually use the "getDefinitionByName" method to get a Class embedded in a clip. This means, however, that your clip must have a class of that name. If you want a generic system to work for all, all your swfs must have a class with the same class name.
I say stick to just using Loaders for now, I'm afraid.
eudora
May 6th, 2007, 11:56 AM
woah.. actually I just saw this... display object container...
I believe it would work perfectly with external loaded swf using loader class..
Just create a container and addChild(loader)
Then when need, i will call.. getChildByName...
This is the example code..
var container:Sprite = new Sprite();
var sprite1:Sprite = new Sprite();
sprite1.name = "sprite1";
var sprite2:Sprite = new Sprite();
sprite2.name = "sprite2";
container.addChild(sprite1);
container.addChild(sprite2);
var target:DisplayObject = container.getChildByName("sprite1");
Will this work?
Dazzer
May 6th, 2007, 12:40 PM
no. because like we mentioned earlier, a displayObject can only have 1 parent. If another container adds it as the child, the object's parent changes. It doesn't create a new copy.
eudora
May 6th, 2007, 08:03 PM
Oh yah.. forgot about that again... hmmm
Dazzer
May 7th, 2007, 03:18 AM
Unfortunately... As3 has been out for so long already. I think if there was a way to do it, someone would have done it already
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.