Skip to main content
Participant
June 15, 2022
Answered

Accessing a variable on the main timeline from a movieclip (HTML CANVAS)

  • June 15, 2022
  • 2 replies
  • 306 views

Hi, I have a variable declared on the main timeline:

 

var myNum= 0;

 

and i have a movieclip that, if allowed to play to a certain frame without interaction, it needs to add up to that variable on the main timeline. I tried using the following code:

 

this.parent.myNum = this.parent.myNum + 1;

 

but it doesn't work and can't seem to find the variable at all. What is the correct syntax for this? Can someone please help? Thanks!

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    Hi.

     

    The most common approach is to make the variable a property of the timeline you want to target.

     

    So instead of writing:

    var myNum = 0;

     

    You would write:

    this.myNum = 0;

     

    And then a child of the target timeline could read or write that property like this:

    this.parent.myNum++;

     

    Or you can use the exportRoot global variable that is automatically created in the publishing process from anywhere to reference the main timeline. Like this:

    exportRoot.myNum++;

     

    I hope it helps.

     

    Regards,

    JC

     

    2 replies

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    June 15, 2022

    Hi.

     

    The most common approach is to make the variable a property of the timeline you want to target.

     

    So instead of writing:

    var myNum = 0;

     

    You would write:

    this.myNum = 0;

     

    And then a child of the target timeline could read or write that property like this:

    this.parent.myNum++;

     

    Or you can use the exportRoot global variable that is automatically created in the publishing process from anywhere to reference the main timeline. Like this:

    exportRoot.myNum++;

     

    I hope it helps.

     

    Regards,

    JC

     

    G248Author
    Participant
    June 15, 2022

    Thank you! I will give this a try!!

    G248Author
    Participant
    June 15, 2022

    I managed to use a dispatchEvent instead of trying to call a variable from the main timeline inside the movieclip, it works perfectly!