Monday, January 19, 2009

adding classes dynamically Part II

A little addition to a previous post on adding symbols dynamically from the library. I was having difficulty adding classes(that weren't in the library) dynamically - they weren't being recognised.

var classRef:Class = getDefinitionByName("Square") as Class;
var shape:IShape= new classRef() as IShape;
//Flash displays: ReferenceError: Error #1065: Variable Square is not defined.


With help from Aaron Beall(on adobe forums) who was having the same problem, i've realised that obviously flash doesn't know to compile the class if it only exists in a string. So we need to ensure that flash compiles every possible class that we may refer to dynamically. We can do this simply by declaring a variable of that class type.

var squareRef:Square;

No comments: