I have an array of ingredients for soup as strings. These ingredients are also movieclips in my library, all linked with an identifier. I want to add the movieclips to the stage dynamically.
I tried getDefinitionByName but I keep getting an error that the variable “appel” (veg[0] in this case) is undefined.
And basically, I only have a general idea what I’m doing here, so I would be grateful if someone could explain it to me or link me to a helpful tutorial.
package {
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.utils.getDefinitionByName;
public class groentesoep extends MovieClip {
const xAr:Array = [13,163,13,26,133,192,169,301,47,313,286,291,163,16];
const yAr:Array = [12,133,133,370,484,352,256,316,241,160,468,10,53,465];
const veg:Array = ["appel","bloemkool","broccoli","brood","champignons","friet","kaas","melk","paprika","prei","tomaat","ui","vis","wortel"];
var i:int;
public function groentesoep() {
// constructor code
create_intro();
}
public function create_intro () {
var bord:intro = new intro();
bord.x = -30;
bord.y = -6;
bord.name = "intro";
addChild(bord);
var btn:begin = new begin();
btn.x = 416;
btn.y = 255;
btn.name = "beginKnop";
btn.addEventListener(MouseEvent.CLICK, create_spel);
addChild(btn);
}
public function create_spel(event:MouseEvent) {
removeChild(getChildByName("intro"));
getChildByName("beginKnop").removeEventListener(MouseEvent.CLICK, create_spel);
removeChild(getChildByName("beginKnop"));
var bord:spel = new spel();
bord.x = -30;
bord.y = -6;
bord.name = "spel";
addChild(bord);
var c:Class = getDefinitionByName(veg[0]) as Class;
var instance:Object = new c();
var mc:MovieClip = MovieClip(instance);
addChild(mc);
}
}}
When using getDefinitionByName, you always have to explicitly use the classes in your code.
That means that if you’re using the class which name is defined in veg [0] – “appel” – you have to “force” the compiler to include that class.
Just wrote at the beginning of your code:
…and do it for all the classes you want to retrieve by their names.
Hope it helps