Thursday, September 25, 2008

Flash CS4 and Flex Metadata

In previous versions of Flash up to Flash CS3 , Flash did not support many of the Flex Metadata such as :

* Embed
* IconFile
* ResourceBundle
* Style

The most common metadata complained about is Embed metadata. Flash CS4 has added support to many of these metadata which will help developers to write code in Flex and testing it in Flash without worrying about incompatibilities at least in this area. In this post am going to talk about Embed metadata and show some examples.

Users can use embed metadata to embed the following type of assets :
* Image (GIF, JPEG, PNG).
* SVG.
* Sounds.
* SWFs (both AS2 and AS3).
* Symbols extracted from SWFs (not sure if AS2 or AS3 or both supported).
* Fonts from AS2 SWFs only (i think, maybe from AS3 SWFs as well).

Example : in the following example am going to embed GIF using Embed metadata.

1. Launch Flash CS4
2. File > New and select "Actionscript File".
3. Copy and paste the following code

package {
import flash.display.*;
public class GIFEmbed extends Sprite {

[Embed(source="../assets/tala.gif")]
private var theClass:Class;

public function GIFEmbed() {

var displayObj:DisplayObject = new theClass();
addChild(displayObj);

}
}
}

4. Save the AS file and give it a same name as the class name " GIFEmbed.as "

Note : [Embed(source="yourAssets.gif")] this is where you point to the actual assets.Please change the yourAssets.gif with the absolute path or relative path to your assets.
5. Create a Fla file and save it in the same place where the AS file is.
6. Add the GIFEmbed.as file as document class.
7. Test Movie.
8. You will get the following warning message as follow :




9. Click "Update Library path" button to add the path of the Flex.SWC to your library path.

10. Test Movie. Your image should be successfully embedded.

9 comments:

  1. Is it possible to use RemoteObject and Class Maping same way? I founded nothing in google)

    ReplyDelete
  2. Any Class that is pure AS3 and does not depends on the Flex framework should work just fine. I am working on using Flex RemoteObject component in Flash that will be posted "hopefully" this month.

    ReplyDelete
  3. Thanks a lot for this post. After hours of searching about embedded assets and the AS3 compiler, I found your post to be the most helpful.

    One important note. If an instance of the GIFEmbed class is created inside a nested movie clip in the .fla, the embedded asset will not display. I do not have an explanation as to why it will not display, but this phenomena is very easy to duplicate through testing.

    However, if the nested MovieClip is exported for actionscript, the embedded asset displays correctly.

    Can anyone offer an explanation for this behavior?

    ReplyDelete
  4. Thank you, Benjamin!!
    Regarding to the problem you're describing, would you please email me a fla file that reproduce the issue or just provide me with steps to reproduce the problem.

    Thanks,
    Tareq

    ReplyDelete
  5. It appears that all my embedded content is loading before the first frame of my swf. it loads before the stage color loads so all users see is white until my large swf loads. Is there a way to force embedded content to load on a different frame?

    thanks!

    ReplyDelete
  6. I tried the code out. It worked with images and a swf but when I tried a large swf (11mb) it broke and gave me an error: 1120: Access of undefined property GIFEmbed_theClass.

    ReplyDelete
  7. I actually never tried it with this size of a SWF. could you please email me your SWF? You can also file a bug report and attache your sample file :

    https://www.adobe.com/cfusion/mmform/index.cfm?name=wishform

    ReplyDelete
  8. I am trying to embed like 100 png files, but it will not compile anymore. Is this a bug, or should i find another solution for this to work?

    I receive no errors, but the swf will not display anything.

    ReplyDelete
  9. @navigatie: I bet you are running out of memory in the compiler and Flash does not say anything which we are hoping to fix in future releases by raising a dialog when that happens.
    You can solve this issues by changing the memory allocated for your AS3 compiler as follow :
    1. Browse to JVM.ini
    Windows :
    C:\Program Files (x86)\Adobe\Adobe Flash CS5\Common\First Run\ActionScript 3.0\jvm.ini

    Mac:
    /Applications/Adobe Flash CS5/Common/First Run/ActionScript 3.0/jvm.ini

    2. when opening the jvm.ini change the 128 to whatever hight and that should fix your problem.
    3. restart flash.

    ReplyDelete