Re: [flexcoders] Flex3: attaching assets from .swf using getDefinitionByName
OK :) Embed assets like this: package com.foo.bar { [Embed('../assets/symbols.swf', symbol='hearts')] public class FooBar extends Sprite { public function FooBar() { super(); } } } Then in your MXML: mx:Button label=1 icon={com.foo.bar.FooBar} / This approach is better because it doesn't create dummy variables. You don't use mx.core.SpriteAsset (which is a redundancy introduced by design in the framework). You can add more functionality to the embedded asset, if needed. Besides, it's easier to debug (you can, for example put trace() in constructor and see how many times this or another asset is used). Sometimes it is a requirement to document assets too, but if you cannot name them as you want - which is the case with embedding on variables, then this is the way to go. Best. Oleg
[flexcoders] Flex3: attaching assets from .swf using getDefinitionByName
Hello, I'm new to this list and also Flex. I'm stuck with a probably simple problem, which I'd like to demonstrate with a short test case: Test.mxml: ?xml version=1.0 encoding=utf-8? mx:Application xmlns:mx=http://www.adobe.com/2006/mxml; layout=absolute creationComplete=onCreationComplete(); mx:Script ![CDATA[ private function onCreationComplete():void { var sprite:Sprite = new Sprite(); var g:Graphics = sprite.graphics; g.lineStyle(1, 0xFF); g.beginFill(0xFF); g.drawCircle(100, 100, 20); g.endFill(); spriteHolder.addChild(sprite); // XXX stuff below not working var suit:String = 'SPADES'; var mc:MovieClip = new (getDefinitionByName(Symbols.CLUBS) as Class); spriteHolder.addChild(mc); } ]] /mx:Script mx:VBox width=100% mx:Button label=1 icon={Symbols.SPADES} / mx:Button label=2 icon={Symbols.CLUBS} / mx:Button label=3 icon={Symbols.DIAMONDS} / mx:Button label=4 icon={Symbols.HEARTS} / mx:UIComponent id=spriteHolder width=200 height=200/ /mx:VBox /mx:Application Symbols.as: package { public class Symbols { [Embed('../assets/symbols.swf', symbol='spades')] public static const SPADES:Class; [Embed('../assets/symbols.swf', symbol='clubs')] public static const CLUBS:Class; [Embed('../assets/symbols.swf', symbol='diamonds')] public static const DIAMONDS:Class; [Embed('../assets/symbols.swf', symbol='hearts')] public static const HEARTS:Class; } } How could I make the mc appear? Everything else work ok. (the icons appear at the buttons, there is a red circle,...) Thank you Alex
Re: [flexcoders] Flex3: attaching assets from .swf using getDefinitionByName
1. You cannot embed on constants. 2. It is always better to embed on a class then on a variable (you won't be using mx.core.WhateverAsset for the asset factory, but a normal flash.* class. Best. Oleg
Re: [flexcoders] Flex3: attaching assets from .swf using getDefinitionByName
Hello, On Wed, Mar 24, 2010 at 11:23 PM, Oleg Sivokon olegsivo...@gmail.comwrote: 1. You cannot embed on constants. 2. It is always better to embed on a class then on a variable (you won't be using mx.core.WhateverAsset for the asset factory, but a normal flash.* class. 1) Are you sure? Embedding seems to work fine for the lines mx:Button label=1 icon={Symbols.SPADES} / mx:Button label=2 icon={Symbols.CLUBS} / mx:Button label=3 icon={Symbols.DIAMONDS} / mx:Button label=4 icon={Symbols.HEARTS} / Also, I've changed from const to var in the package { public class Symbols { [Embed('../assets/symbols.swf', symbol='spades')] public static var SPADES:Class; and still get the same runtime error ReferenceError: Error #1065: Variable SPADES is not defined. at global/flash.utils::getDefinitionByName() at TestCase/onCreationComplete()[C:\Documents and Settings\afarber\My Documents\Flex Builder 3\TestCase\src\TestCase.mxml:21] at TestCase/___TestCase_Application1_creationComplete()[C:\Documents and Settings\afarber\My Documents\Flex Builder 3\TestCase\src\TestCase.mxml:4] 2) Could you elaborate, I don't understand what you've written. Can anyone please fix my test code? Here is it again, it is very short: package { public class Symbols { [Embed('../assets/symbols.swf', symbol='spades')] public static var SPADES:Class; [Embed('../assets/symbols.swf', symbol='clubs')] public static var CLUBS:Class; [Embed('../assets/symbols.swf', symbol='diamonds')] public static var DIAMONDS:Class; [Embed('../assets/symbols.swf', symbol='hearts')] public static var HEARTS:Class; } } - ?xml version=1.0 encoding=utf-8? mx:Application xmlns:mx=http://www.adobe.com/2006/mxml; layout=absolute creationComplete=onCreationComplete(); mx:Script ![CDATA[ private function onCreationComplete():void { var sprite:Sprite = new Sprite(); var g:Graphics = sprite.graphics; g.lineStyle(1, 0xFF); g.beginFill(0xFF); g.drawCircle(100, 100, 20); g.endFill(); spriteHolder.addChild(sprite); // XXX stuff below not working, can it be fixed? var suit:String = SPADES; var mc:MovieClip = new (getDefinitionByName(Symbols.SPADES) as Class); spriteHolder.addChild(mc); } ]] /mx:Script mx:VBox width=100% mx:Button label=1 icon={Symbols.SPADES} / mx:Button label=2 icon={Symbols.CLUBS} / mx:Button label=3 icon={Symbols.DIAMONDS} / mx:Button label=4 icon={Symbols.HEARTS} / mx:UIComponent id=spriteHolder width=200 height=200/ /mx:VBox /mx:Application