1
votes

i have a little problem with getDefinitionByName. My purpose is to instantiate an FXG object(Number10.fxg) in a document mxml on runtime. The name of the Class is in a string variable that is used by getDefinitionByName to return the name of the class to insantiate. The code doesn't work even if doesn't send an error message. The code is as follows:

import assets.Number10;
import flash.utils.getDefinitionByName;
import mx.core.IVisualElement;

private function onClick(event:MouseEvent):void
{ 
  var value:String = "Number10";
  var ClassDefinition:Class = getDefinitionByName(value) as Class;
  var ten:IVisualElement = new ClassDefinition() as IVisualElement;
  this.contentGroup.addElement(ten);
}

I tried also with... var ten:IVisualElement = new ClassDefinition(); but nothing. It Doesn't work! Please, Help me!


First of all, i refer to the adobe documentation pages that covering the topic so telegraphic. Here it is:

Option includes class [...]

Description Links one or more classes to the resulting application SWF file, whether or not those classes are required at compile time. To link an entire SWC file rather than individual classes, use the include-libraries option.

Ok.In Flash Builder i go to the Additional compiler arguments where there is just this option

-locale en_US

So i add my option under this

-includes class = assets.Number10

or

-includes class assets.Number10

or

-includes class Number10

When the application runs i get the Error #2032.

I think that the option declaretion is wrong. I do not have a good reference for using option. So...Help me!

How can i declare the Number10 class or the assets package with the other fxg object using the includes class option?

3

3 Answers

1
votes

Ok! I find the solution... Is to put a reference to Number10 class somewhere in the code, for instance:

import assets.Number10;
import flash.utils.getDefinitionByName;
import spark.core.SpriteVisualElement;

//case1
var myNumber:Number10;

//or

//case2
Number10;

private function onClick(event:MouseEvent):void
{ 
  var value:String = "assets.Number10";
  var ClassDefinition:Class = getDefinitionByName(value) as Class;
  var ten:SpriteVisualElement = new ClassDefinition() as SpriteVisualElement;
  this.contentGroup.addElement(ten);
}

and the code works :-)

This is a problem that comes from the way that Flex compiles its code. Flex compiles its code so that if a class is not used, it will keep this class off the final compiled program. But the problems are not over yet! If i have hundreds of Fxg objects that could be instantiate, declaring all classes is little difficult and tedious. So, how i can delclare in one time all classes of a package?

1
votes

You can add classes to SWCs and SWFs using the include and includeClasses compiler options. Using these, you don't have to reference the classes in the code. Consult the documentation for proper usage.

0
votes

Be sure to use the fully qualifed class name.

Also, the approach of casting your FXG class as an IVisualElement is new to me. I thought you had to use real classes in casting and the sort. Try using a SpriteVisualElement.

private function onClick(event:MouseEvent):void
{ 
  var value:String = "assets.Number10";
  var ClassDefinition:Class = getDefinitionByName(value) as Class;
  var ten:IVisualElement = new ClassDefinition() as SpriteVisualElement.;
  this.contentGroup.addElement(ten);
}