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

HTML5 Canvas - Accessing variables across main timeline

Community Beginner ,
Nov 24, 2016 Nov 24, 2016

Copy link to clipboard

Copied

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?

Views

4.4K

Translate

Translate

Report

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.

Votes

Translate

Translate
LEGEND ,
Nov 24, 2016 Nov 24, 2016

Copy link to clipboard

Copied

I believe that:

this.varname = value;

would be seen across the current timeline, and:

window.varname = value;

would be seen everywhere.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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();

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

I have sussed it

this.myVar = "Paul";

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

this.whatIsMyVariable = function()

{

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

}

this.whatIsMyVariable();

Votes

Translate

Translate

Report

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