Skip to main content
Daniel Stamp
Participating Frequently
February 20, 2019
Answered

Accessing movieclips within movieclips in a frame's code. (Canvas / Javascript)

  • February 20, 2019
  • 1 reply
  • 2452 views

Hi, and apologies if this is an easy question. I've been trying various things all day to no avail.

if I create an movie clip in canvas mode in Animate, and name it "Object1", and then inside that, make another movie clip named "Object2", I seem to be unable to access "Object2" at the start of a frame.  For example, see the following code:

var _this = this;

console.log(_this.object1); // Prints an actual object to the console log. You can even see Object 2 inside there with values.

console.log(_this.object1.object2); // Undefined.

   

However, if I add it a on click function, as shown below, it works.

_this.on('click', function()

{

    console.log(_this.object1); // Prints object 1 as above.

    console.log(_this.object1.object2); // print object 2.

});

Does anyone know why it does this, and how I'm meant to get around it?  It's basic functionality that I've been used to in flash, but I'm not sure how it works in animate.  I figure it's a timing thing? perhaps object 2 isn't ready on time? But I can't find any documentation on this at all.

Thanks

This topic has been closed for replies.
Correct answer kdmemory

Hi Daniel

if you would do the following in frame 1

var _this = this;

_this.timeoutFunction = function() {

    console.log(_this.Object1);

    console.log(_this.Object1.Object2);

};

setTimeout(_this.timeoutFunction, 0);

that works. It's like with click, the setTimeout comes later in the exucution chain once everything is available.

Klaus

1 reply

kdmemory
kdmemoryCorrect answer
Inspiring
February 20, 2019

Hi Daniel

if you would do the following in frame 1

var _this = this;

_this.timeoutFunction = function() {

    console.log(_this.Object1);

    console.log(_this.Object1.Object2);

};

setTimeout(_this.timeoutFunction, 0);

that works. It's like with click, the setTimeout comes later in the exucution chain once everything is available.

Klaus

Daniel Stamp
Participating Frequently
February 20, 2019

Thanks! Is that the recommended way of doing things in Adobe Animate? It seems like a bit of a bodge!

avid_body16B8
Legend
February 20, 2019

This is what I do. I put all my code in frame 1 and use

var root = this;

which is the same, just a different variable name. (I prefer root because it is more distinguishable from the regular this when you read code).