Skip to main content
stuartf76130223
Inspiring
November 29, 2016
Answered

In animate CC, How do you target a root instance named object inside a function?

  • November 29, 2016
  • 2 replies
  • 3189 views

Have a html5 canvas document.

I have an object called "brick2" in the root of an animate CC scene (with the same instance name).

Code looks like this:

function moveBrick() {

        this.brick2=0;

    }

var timer1 = setInterval(moveBrick, 100);

this.stop();

When I run the file in a browser the brick doesn't go to "0" x position. However if I write this script (putting the x position property outside the function) it all works fine:

function moveBrick() {       

    }

var timer1 = setInterval(moveBrick, 100);

this.brick.x=0;

this.stop();

Any ideas on what I am doing wrong? Have a real worry that is something completely obvious.

This topic has been closed for replies.
Correct answer Colin Holgate

The 'this' inside the function isn't referring to the timeline. Here's one way around that:

var self = this;

function moveBrick() {

        self.brick2=0;

    }

var timer1 = setInterval(moveBrick, 100);

this.stop();

2 replies

Colin Holgate
Colin HolgateCorrect answer
Inspiring
November 29, 2016

The 'this' inside the function isn't referring to the timeline. Here's one way around that:

var self = this;

function moveBrick() {

        self.brick2=0;

    }

var timer1 = setInterval(moveBrick, 100);

this.stop();

Inspiring
November 29, 2016

Doesn't CreateJS specifically make a variable to track the root timeline too? exportRoot or something like that?

e.g. exportRoot.brick2.x should work globally too, if I'm recalling things right…

Jon Fritz
Community Expert
Community Expert
November 29, 2016

Moved from the Forum Lounge to the Adobe Animate CC - General  forum.