Skip to main content
Participant
March 21, 2022
Answered

Html5 canvas variable in different frames

  • March 21, 2022
  • 2 replies
  • 216 views

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 has been closed for replies.
Correct answer Colin Holgate

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.

 

2 replies

Participant
March 22, 2022

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

Colin Holgate
Colin HolgateCorrect answer
Inspiring
March 21, 2022

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.

 

Participant
March 22, 2022

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!!