PDA

View Full Version : How to access properties of symbols in loaded SWF?


invision-studios
06-30-2008, 04:02 PM
I'm making the migration from Flash to Flex, but I'm running into a dead end on one particular area: accessing and manipulating properties of symbols in a loaded SWF.

The SWF is actually a simple library of a detailed parts illustration. I would like to load the entire SWF in position, and be able to change the attributes (i.e. colors) of individual symbols (or parts of the illustration). I can load, display and manipulate each symbol separately, but I need the parts to remain in place.

The original art was created in Illustrator and exported as SWF using the symbols palette.

I can always go back to Flash, but I'm forcing myself to move forward and do this project in Flex (baptism by fire!!). I'm working in Flex Builder 3/CS3.

Is there some sort of dot notation to access the symbols from within the SWF, without loading them all separately?

Let me put this another way: it appears that I can load an SWF and then instantiate each symbol in that SWF, and then load each symbol to the 'stage' (or whatever it's called in Flex) separately.

I can certainly instantiate each symbol separately, and then align it on stage, but it seems to me to be bass-ackward to do it that way, expecially since I created the original SWF with everything in position.

Is there no way to access the attributes of the individual symbols within an SWF without separately instantiating them?

I'm just having trouble getting my head around this. Any help is greatly appreciated.

Zunskigraphics
07-02-2008, 03:37 PM
I'm looking for a solution to this as well. Anyone?

matlevy
07-03-2008, 12:41 AM
Load the SWF into a SWFLoader component and add an event listener to the loaderInfo property, eg


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
private function init():void
{
this.loader.loaderInfo.addEventListener(Event.INIT , onLoaderInit);
}

private function onLoaderInit(e:Event):void
{
var content : DisplayObject = DisplayObject(e.currentTarget);
content.myProperty
}
]]>
</mx:Script>
<mx:SWFLoader source="myswf.swf" id="loader"/>
</mx:Application>

invision-studios
07-04-2008, 01:32 PM
Matthew,

I appreciate your reply, but you lost me.

Looks like your script loads the SWF, checks to see if the load is complete, then gets the properties of the loaded SWF?

I'm trying to get at named symbols in the loaded SWF. Wouldn't I have to instantiate the symbols in some fashion before I could get/set their attributes?

Sly_cardinal
07-04-2008, 01:51 PM
As matlevy posted you should be able to reference the clip instances in the loaded swfs through the SWFLoader.content property - that is, if the symbols are on the stage of the loaded swf (in matlevy's example, you would be referencing a DisplayObject called 'myProperty').

Are you attempting to access MovieClips that have already been instantiated, or are you trying to instantiate symbols from the library?

invision-studios
07-04-2008, 02:39 PM
The symbols are already on the stage in the SWF.

I'm an experienced Flash and Illustrator designer, experienced javascript coder, but fairly new to doing anything in AS beyond simple interactivity, so I'm trying to get my head around this whole thing.

What I'm missing in Matthew's code is how he's able to get/set attributes of any named instance in the SWF file. He'd declaring var 'content', but it doesn't appear to link to any specific instance of a symbol.

Can you offer a concrete example? Examples have been too abstract for me so far.

Let's assume I load 'house.swf' and on the stage in that file I have instances of several symbols, 'mainHouseOutline' 'roof' 'door' 'window' and 'shutter'. How do I connect with their individual properties?

Sly_cardinal
07-04-2008, 03:22 PM
OK, I've got a simple test app working. I've confirmed that you can just access the MovieClip instances straight from the loaded swf and interact with them.

The key is accessing the content of the SWFLoader - that's the stage of the loaded swf.


var content:DisplayObjectContainer = DisplayObjectContainer(SWFLoader(event.target).con tent);


Example code attached (sorry, my Flex Builder trial ran out) - I'm using FlashDevelop (which is actually really quite reasonable for flex development).

There is nothing particularly complex happening - I'm just loading the FlashAssets.swf into the flex app then accessing the MovieClips instances that are on the stage of the loaded clip (drag the black box).

invision-studios
07-05-2008, 12:36 AM
Sly,

thanks for going to all this trouble, I'll try out your code and post the results back here.

Sherm

mattb
07-08-2008, 04:23 AM
Check out gSkinner's excellent page on the subject here:

http://www.gskinner.com/blog/archives/2007/03/using_flash_sym.html

invision-studios
07-08-2008, 05:38 PM
Thanks for the reference. I saw Skinner's symbols library and may eventually implement it, but I was trying to learn to walk before I run!

invision-studios
07-08-2008, 05:52 PM
Okay, I took a look at this and it's beginning to make sense.

Assuming I have a SWF named 'FlashAssets.swf' and two MovieClips called 'clip1' and 'clip2' in the SWF file.

In my Flex document I have a SWFLoader object:

<mx:SWFLoader id="swfLoader" />

then, with actionscript, I associate a loaded SWF with the SWFLoader object:

swfLoader.source = "FlashAssets.swf";

after the SWF is loaded, I can associate variables with the content of the SWFLoader object (after checking to be sure it's loaded):

var content:DisplayObjectContainer = DisplayObjectContainer(SWFLoader(event.target).con tent);
var clip1:MovieClip = MovieClip(content["clip1"]);
var clip2:MovieClip = MovieClip(content["clip2"]);

I'm getting it to do what I want when I hard-wire the values for changing color of one of the clips.

I'm now trying to figure out how to populate an array with all the clips in the SWF and call them. I'll post the question in a separate thread.

Sly_cardinal
07-09-2008, 06:39 AM
Check out gSkinner's excellent page on the subject here:

http://www.gskinner.com/blog/archives/2007/03/using_flash_sym.html

I think that's a slightly different case - when you embed external swfs into your flex swf at compile-time.

We are loading the swf in a runtime so flex doesn't strip out the actionscript.