Skip to main content
Inspiring
November 11, 2019
Answered

Animate - Built in functions?

  • November 11, 2019
  • 3 replies
  • 1616 views

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

_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.

 

3 replies

Legend
November 12, 2019

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

 

 

Legend
November 11, 2019

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?

 

https://createjs.com/docs/easeljs/modules/EaselJS.html

GmRAuthor
Inspiring
November 11, 2019

Ok thanks.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
November 11, 2019

_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.

 

GmRAuthor
Inspiring
November 11, 2019

Thanks for taking the time to explain that kglad... very helpful.

 

Kind regards,

Geoff

kglad
Community Expert
Community Expert
November 11, 2019

you're welcome.