- [Embed(parameter1, paramater2, ...)] metadata tag
You use this syntax to embed an asset in an ActionScript file, or in an <mx:script> block in an MXML file. - @Embed(parameter1, paramater2, ...) directive
You use this syntax in an MXML tag definition to embed an asset. - Embed(parameter1, paramater2, ...) directive
You use this syntax in an <mx:Style> block in an MXML file to embed an asset.
It's kind of tricky to figure out how to access these embedded assets from actionscripts, I had to spend precious time figuring out how to use these assets from Actionscripts, especially for these actionscript classes under specific packages.
It turns out the solution is fairly straight, what you need to make it work is accessing assets using the correct path, assume that all assets are under assets directory which is a subdirectory of your application root. Here is a sample root directory, the root is helloworld and assets are under assets subdirectory, helloUtil.as is with hello.util package:
helloworld/helloword.asnow you can access these two images in helloworld.as:
/assets/bg.jpg
/assets/ball.png
/hello/util/helloUtil.as
[Embed(source="assets/bg.jpg")]Without putting assets in the correct directory you will get the following compiling errors:
public var bgImg:Class;
[Embed(source="assets/ball.png")]
public var ballImg:Class;
Error: unable to resolve 'bg.jpg' for transcoding
Now if you structure your classes using packages and you also want to access assets directly, then you must access them like:
[Embed(source="/assets/bg.jpg")]Pretty straight, huh, but for a novice it will take sometime to figure this out. And I am hoping you will benefit from it
public var bgImg:Class;
[Embed(source="/assets/ball.png")]
public var ballImg:Class;
No comments:
Post a Comment