我正在尝试用javascript编写游戏,但我遇到了一些问题。
我有一个游戏管理器功能,我添加了几个原型功能。
一个是调用另一个,我使用了this.function,但它仍然无法正常工作。
我确信它是一个简单的解决方案,但我已经找了几个小时的解决方案,但无法解决它。
问题是上一个函数start_round
调用generate_question
并继续获取Game.js:117 Uncaught TypeError: this.generate_question is not a function
gameManger = function(display_img,button1, button2, button3, button4, counter_display, r) {
this.current_mineral = ''
this.timer = null
var display_img = display_img
var counter_display = button1
var button1 = button1
var button2 = button2
var button3 = button3
var button4 = button4
var r = r
}
gameManger.prototype.getRandomMineral = function() {
if (!this.minerals.length) {
return null;
}
var index = Math.floor(Math.random() * this.minerals.length);
var val = this.minerals[index];
this.minerals.splice(index, 1);
this.current_mineral = val;
return val;
}
gameManger.prototype.getCurrentMineral =function() {
return this.current_mineral;
}
gameManger.prototype.generate_question = function () {
var random_mineral = this.game_manger.getRandomMineral();
var choosen_answers = getPossibleAnsweres()
displaysrc = "minerals-images//"+this.minerals_img[this.minerals[random_mineral]]
this.button1.firstChild.data = choosen_answers[r.get()]
this.button2.firstChild.data= choosen_answers[r.get()]
this.button3.firstChild.data = choosen_answers[r.get()]
this.button4.firstChild.data = choosen_answers[r.get()]
this.r.reset();
}
gameManger.prototype.getPossibleAnsweres = function(){
var choosen_answers = []
choosen_answers.push(this.game_manger.getCurrentMineral())
while(choosen_answers.length < 4){
possible_answer = this.minerals[Math.floor(Math.random() * this.minerals.length)]
if(choosen_answers.indexOf(possible_answer) <= -1){
choosen_answers.push(possible_answer)
}
}
return choosen_answers;
}
gameManger.prototype.start_round = function () {
var seconds = 0
timer = setTimeout(function () {
if (--seconds < 0) {
seconds = this.level;
this.generate_question();
}
if(this.minerals <= 0){
clearInterval(timer)
return;
}
this.counter_display.firstChild.data = seconds;
}, 1000);
}