2

I need some sorcery to get children of an object that I've just instantiated. Here's the story:

So I have an Animate CC and I'm making a MovieClip with few children inside. After I'm happy with it I'm setting Linkage in the library to FooClip and publish the scene. Now I want to edit JS file and add some magic. Let say instantiate few objects and different child in each one. Code looks more or less like that:

for (var i = 0; i < 10; i++) {
    var foo = new lib.FooClip();
    var child = foo.getChildAt(i);
    console.log(child);                // This prints out `undefined`
    child.alpha = 0.5;
    foo.x = i * 10;
    stage.addChild(foo);
}

And obviously, I'm getting an error in here...

Main.js:58 Uncaught TypeError: Cannot set property 'alpha' of undefined

After adding some more logs and setting breakpoints I can see that after instantiating new object I don't have any children yet. Also console.log(foo.children); is showing me absolutely nothing...

However... Adding dirty little hack: setTimeout(function(){ console.log(foo.children); }, 200); inside my for loop is printing me out all children. So it's like I need to wait until object actually instantiates and create all the crap inside before I can access it. Back in the good old Flash days I remember waiting for ADDED_TO_STAGE event, but I don't see anything like that in here :/ (there's added but it's not helping).

Anyone know how to solve it? Did I forget about something obvious?

Florian Schaal
  • 2,586
  • 3
  • 39
  • 59
3vilguy
  • 981
  • 1
  • 7
  • 20

1 Answers1

3

I am pretty sure this is due to a bug in the Animate CC export, where children aren't available immediately.

Try this:

var foo = new lib.FooClip();
foo.gotoAndStop(0); // Force an update
var child = foo.getChildAt(i);

There should be a fix for this in an upcoming version.

Lanny
  • 11,244
  • 1
  • 22
  • 30