0

I am trying to write a game in javascript, but i have been having some problems. I have a game manager function, and i added few prototypes functions to it. One is calling the other, i used this.function, but it still wont work. I am sure its a simple solution, but i have looked for a solution for hours but couldn't solve it. Problem is with last function start_round that calls generate_question and keep getting 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);
}  
user2314737
  • 27,088
  • 20
  • 102
  • 114
Gigalala
  • 439
  • 1
  • 8
  • 24
  • what? with its name? – Gigalala Sep 29 '16 at 18:23
  • You need to call it in full name? all the examples i saw were not like that – Gigalala Sep 29 '16 at 18:23
  • Ah, looks like it's time to go to bed. `this` in the callback passed to `setTimeout` doesn't refer to an instance. You've to capture the context into a variable in the outer scope, or use [`bind`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind). – Teemu Sep 29 '16 at 18:37
  • Notice that `getPossibleAnsweres()` needs to be `this.getPossibleAnsweres()` – Bergi Sep 29 '16 at 19:04

0 Answers0