Skip to main content
Participant
Mar 21, 2022 9:45 PM UTC
Answered

Html5 canvas variable in different frames

My question is regarding actionscript/javascript in an adobe animate canvas/html5 project.

I have the following code:

 

Main timeline, Frame 0:

     var tempArray = [1,2,3,4,5];

     alert(tempArray[2]);     // alert shows 3

Main timeline, Frame 50:

     tempArray[2] = 3;     

     alert(tempArray[2]);     // alert is ignored and doesnt print anything 

 

WHY?

 

This topic is closed to new replies. Start a new post to keep the conversation going.
Colin Holgate
Colin HolgateCorrect answer
Inspiring
Mar 21, 2022 10:09 PM UTC

var variablename only belongs to this frame. When you get to frame 50 and talk to a variable with the same name, it doesn't really exist.

If you want to stash values to pick up in a different context, you could make the variable belong to something higher in the hierarchy. For example, this would work:

exportRoot.tempArray = [1,2,3,4,5];
alert(exportRoot.tempArray[2]);     // alert shows 3
//Main timeline, Frame 50:
exportRoot.tempArray[2] = 8;     
alert(exportRoot.tempArray[2]);

would alert 3 and then 8.

 

3 replies

Participant
Mar 22, 2022 1:15 AM UTC

Simple fix!  You're awesome... Thank you!!

Translate
Colin Holgate
Colin HolgateCorrect answer
Inspiring
Mar 21, 2022 10:09 PM UTC

var variablename only belongs to this frame. When you get to frame 50 and talk to a variable with the same name, it doesn't really exist.

If you want to stash values to pick up in a different context, you could make the variable belong to something higher in the hierarchy. For example, this would work:

exportRoot.tempArray = [1,2,3,4,5];
alert(exportRoot.tempArray[2]);     // alert shows 3
//Main timeline, Frame 50:
exportRoot.tempArray[2] = 8;     
alert(exportRoot.tempArray[2]);

would alert 3 and then 8.

 

Translate
Participant
Mar 22, 2022 1:17 AM UTC

Its interesting that I can use the this.parent to reference a variable from another clip but no way to reference the same variable from the same timeline.  Crazy!!

Translate