Copy link to clipboard
Copied
Hi, I'm converting an old Actionscript 1 Flash project to HTML5 Canvas (Javascript), and wondering if there are any built-in functions or global variables I can access, such as the Timeline's current frame number?
I have tried the following, but it is alerting as 'frameNumber=undefined'
/*
Get the frame number of current frame
*/
var frameNumber = _this.currentFrame;
alert("frameNumber="+this.frameNumber);
POST SCRIPT:
I changed the above to
/*
Get the frame number of current frame
*/
var frameNumber = _this.currentFrame;
alert("frameNumber="+frameNumber);
and it worked. I don't udnerstand the use of or scope of the "this". Would someone be willing to explain it?
Also, do both these lines of code need to be inserted in every frame I want it displayed, or is there a global function way of accessing the framenumber, with simply (something like):
alert("frameNumber="+frameNumber);
Thanks,
Geoff
_this means nothing in canvas unless you define it. eg, the snippets panel usually does something like
var _this = this;
this refers to the entire timeline and is not frame dependent. eg,
this.var1 = "var1";
this.var1 is defined on every frame of the timeline that comtains the code.
var var2 = "var2";
is defined only on the frame that contains that code. ie, trying to reference var2 in a different frame will trigger and error.
also, note there's a global script panel where you ca
...Copy link to clipboard
Copied
_this means nothing in canvas unless you define it. eg, the snippets panel usually does something like
var _this = this;
this refers to the entire timeline and is not frame dependent. eg,
this.var1 = "var1";
this.var1 is defined on every frame of the timeline that comtains the code.
var var2 = "var2";
is defined only on the frame that contains that code. ie, trying to reference var2 in a different frame will trigger and error.
also, note there's a global script panel where you can use something like:
var var3 = "var3";
and it will be defined in every frame of every timeline in that canvas project.
Copy link to clipboard
Copied
Thanks for taking the time to explain that kglad... very helpful.
Kind regards,
Geoff
Copy link to clipboard
Copied
you're welcome.
Copy link to clipboard
Copied
Ok, I have placed this in my Global Script panel:
var frameNumber = this.currentFrame;
and on frame 1 of Timeline added this code:
alert("frameNumber="+frameNumber);
But it is still undefined. I have also tried the above with and without the ".this".
What am I doing wrong?
Thanks,
Geoff
Copy link to clipboard
Copied
attach a screenshot showing your global panel and the code.
p.s. that will always show the currentFrame (eg, 0) when the code executes. ie, it won't update as the frames change.
Copy link to clipboard
Copied
Sure, here:
Global:
Frame 1:
Thanks.
Copy link to clipboard
Copied
oops, it's a global script, not a frame script so currentFrame is meaningless (ie, isn't defined) in the global panel. further, "this" references the main window object, not a movieclip/timeline.
use something that doesn't poll the timeline or frame in the global panel.
Copy link to clipboard
Copied
Of course there are built-in functions and variables. Wouldn't be much point to having a scripting language if there weren't, would there?
Copy link to clipboard
Copied
Ok thanks.
Copy link to clipboard
Copied
> is there a global function way of accessing the framenumber
There is no "the" frame number for "the" timeline. The root timeline and every single movie clip on the stage all have their own timelines and frame numbers. If you want a frame number, you have to specify which timeline you want it from.
That being said, if you want to globally access the root frame number, that's accessible from the automatically-created global alias to the root timeline, exportRoot.
alert(exportRoot.currentFrame);