Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

gotoAndPlay not working from inside function

New Here ,
Jun 10, 2020 Jun 10, 2020

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

TOPICS
Code , Timeline
260
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 10, 2020 Jun 10, 2020
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines