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?
I believe that:
this.varname = value;
would be seen across the current timeline, and:
window.varname = value;
would be seen everywhere.
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.
Copy link to clipboard
Copied
This might help...
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.
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.
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();
Copy link to clipboard
Copied
I have sussed it
this.myVar = "Paul";
alert ("this.myVar = " + this.myVar);
this.whatIsMyVariable = function()
{
alert ("function this.myVar = " + this.myVar);
}
this.whatIsMyVariable();