Copy link to clipboard
Copied
Hi – I've just started with playing around with javascript in Animate (not sure it's even the right software for the animation I want to do yet!), but am having a beginners' issue with getting functions to run. I've adapted some of the code from this tutorial.
When I use this code in the first keyframe to change the text on a Dynamic Text box called 'yearText' it works fine:
var currentYear = 2017;
var year = 1971;
this.yearText.text = year;
this.playBtn.addEventListener("click", playClicked.bind(this));
function playClicked() {
this.play();
isPlaying = true;
year = year + 1;
this.yearText.text = year;
}
But then if I put a function inside the function in place of the last two lines like this, it no longer works:
this.playBtn.addEventListener("click", playClicked.bind(this));
function playClicked() {
this.play();
isPlaying = true;
year = year + 1;
this.yearText.text = year;
}
function yearIncrease() {
year = year + 1;
this.yearText.text = year;
}
Have I completely misunderstood something about how functions work?
Any help very much appreciated! Then i can see if Animate is something I'll be able to use properly...
I thnk you misquoted yourself, and didn't have yearIncrease() in the later function.
That aside, the bind(this) part makes the first function use the 'this' value from the main timeline, and in going to another level of function you've lost track of 'this'.
An easy solution is to have a variable pointing to the field. This works:
var currentYear = 2017;
var year = 1971;
var yearTextField = this.yearText;
yearTextField.text = year;
this.playBtn.addEventListener("click", playClicked.bind(this));
function
...Copy link to clipboard
Copied
I thnk you misquoted yourself, and didn't have yearIncrease() in the later function.
That aside, the bind(this) part makes the first function use the 'this' value from the main timeline, and in going to another level of function you've lost track of 'this'.
An easy solution is to have a variable pointing to the field. This works:
var currentYear = 2017;
var year = 1971;
var yearTextField = this.yearText;
yearTextField.text = year;
this.playBtn.addEventListener("click", playClicked.bind(this));
function playClicked() {
this.play();
isPlaying = true;
yearIncrease();
}
function yearIncrease() {
year = year + 1;
yearTextField.text = year;
}
Copy link to clipboard
Copied
So helpful – hadn't got my head round defining text boxes as variables!
thanks very much...
Copy link to clipboard
Copied
and yes I did misquote myself, so even more well spotted...