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

HTML5 Canvas - Accessing variables across main timeline

Community Beginner ,
Nov 24, 2016 Nov 24, 2016

If I set a function or variable on the first frame of the main timeline I don't appear to be able to access them on later frames.

So for example if on frame 1 I set the following variable:

var rootRef = this;

And the following function

function test()

{

     alert ("test");

}

If I try to refer to the variable rootRef on say frame 50, it is undefined

And if I try to call the test function I get the following error:
Uncaught ReferenceError: test is not defined(…)

I would ideally like to include as much code as possible on frame 1 so any advice how to make variables and functions available across the timeline?

4.9K
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 24, 2016 Nov 24, 2016

I believe that:

this.varname = value;

would be seen across the current timeline, and:

window.varname = value;

would be seen everywhere.

Translate
LEGEND ,
Nov 24, 2016 Nov 24, 2016

I believe that:

this.varname = value;

would be seen across the current timeline, and:

window.varname = value;

would be seen everywhere.

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 24, 2016 Nov 24, 2016
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 24, 2016 Nov 24, 2016

Although the last example of myVar = value should work globally, I've seen that lead to errors. By explicitly saying window.myVar = value the errors don't happen.

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 24, 2016 Nov 24, 2016

Thanks Colin. I've had no problems with it so far but good to know to how solve it if I come across it.

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
Community Beginner ,
Nov 25, 2016 Nov 25, 2016

Thank you Colin and Gory.

How do I access a clip level variable within a function?

this.myVar = "Paul";

alert ("this.myVar = " + this.myVar);

function whatIsMyVariable()

{

  alert ("myVar = " + myVar);

}

whatIsMyVariable();

Trying the above gives me an error saying myVar is undefined.

If I make it a global as follows it works but I prefer to minimise the use of global variables

myVar = "Paul";

alert ("myVar = " + myVar);

function whatIsMyVariable()

{

  alert ("myVar = " + myVar);

}

whatIsMyVariable();

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
Community Beginner ,
Nov 25, 2016 Nov 25, 2016
LATEST

I have sussed it

this.myVar = "Paul";

alert ("this.myVar = " + this.myVar);

this.whatIsMyVariable = function()

{

  alert ("function this.myVar = " + this.myVar);

}

this.whatIsMyVariable();

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