Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Explorer ,
Nov 29, 2016 Nov 29, 2016

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.

3.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Nov 29, 2016 Nov 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();

Translate
Community Expert ,
Nov 29, 2016 Nov 29, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 29, 2016 Nov 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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 29, 2016 Nov 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…

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 29, 2016 Nov 29, 2016
LATEST

Great! that works - Thanks so much.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines