Copy link to clipboard
Copied
Hi
I have a problem getting gotoAndPlay working from inside a function called checkScore()? If I use alert() instead of 'this.gotoAndPlay("end"); ' it works correctly??
Sorry my coding ability is pretty basic.. I just can't get my head around why it doesn't work? Is it a problem with 'this'? Thanks for any help 😆 Martin
var num01 = Math.floor((Math.random() * 100) + 10);
var num02 = Math.floor((Math.random() * 100) + 10);
var score = 0;
var totalScore = 0;
this.number1_txt.text = num01;
this.number2_txt.text = num02;
this.check_btn.addEventListener("click", checkAnswer.bind(this));
function checkAnswer(){
var result = document.getElementById("result_txt").value;
if(result == num01+num02){
this.feedback_txt.text = "correct the answer is " + (num01+num02);
this.score_txt.text = score +=1;
num01 = Math.floor((Math.random() * 100) + 10);
num02 = Math.floor((Math.random() * 100) + 10);
this.number1_txt.text = num01;
this.number2_txt.text = num02;
this.anim01_mc.gotoAndPlay("smile_lb");
this.totalScore_txt.text = totalScore +=1;
checkScore();
}else{
this.feedback_txt.text = "wrong the correct answer is " + (num01+num02);
num01 = Math.floor((Math.random() * 100) + 10);
num02 = Math.floor((Math.random() * 100) + 10);
this.number1_txt.text = num01;
this.number2_txt.text = num02;
this.anim01_mc.gotoAndPlay("sad_lb");
this.totalScore_txt.text = totalScore +=1;
checkScore();
}
}
function checkScore(){
if(totalScore >= 10){
setTimeout(function(){
this.gotoAndPlay("end");
}, 1000);
}
}
this.stop();
Copy link to clipboard
Copied
try and see if this help. It is a scope problem in the function. use root to make it global.
https://drive.google.com/file/d/1MruA3DO_HPVLJAfTLNe051wigGGNYK5m/view?usp=sharing