Search code examples
actionscript-3flash

addChild with looping, different name, and access variable


here is my code

stop();
var card:mc;
for (var c:int = 1; c <= 2; c++){
    card = new mc()
    card.name = "card"+c
    addChild(card);
    this["card" + c].gotoAndStop(c);
    trace(["card" + c].var1);
    trace(["card" + c].var2);
}

in the movie clip i have two variable called var1 and var2 in the frame 1 and 2

but I get Error #1010: A term is undefined and has no properties. at Untitled_fla::MainTimeline/frame1()


Solution

  • Display object name is not the same as field name inside the parent object. Normally you use getChildByName(name) and typecasting. In your code you don't need it as your object is already assigned to a local variable:

    stop();
    var card:mc;
    for (var c:int = 1; c <= 2; c++){
        card = new mc()
        card.name = "card"+c
        addChild(card);
        card.gotoAndStop(c);
        trace(card.var1);
        trace(card.var2);
    }