0

please help, I cant find out why this is throwing the error. I've tried everything I can think of, why is this.runLoop() defined and suddenly this.processInput() not, I'm trying to understand classes better and I finally thought I got it, I guess not

    
class Game {
    constructor(teamSize){
        this.cpu1 = new Array(teamSize-1);
        this.cpu2  = new Array(teamSize);
        this.run = -1;
        this.init();  
    }

    init(){
    }


    shutdown(){};

    processInput = function(elapsed) {
      console.log(elapsed);
    }
    updateGame(elapsed){}
    generateOutput(elapsed){
    }

    runLoop(timestamp){
        let last;
        if(last === undefined){
            last = timestamp;
        }
        let elapsed = timestamp - last;

        this.processInput(elapsed);
        this.updateGame(elapsed);
        this.generateOutput(elapsed);
        
        last = timestamp;
        if(this.run == 1){
        window.requestAnimationFrame(this.runLoop);
        }
    };

  start(){
        this.run *= -1;
        if(this.run == 1){
        window.requestAnimationFrame(this.runLoop);
        }
    }
   
};

var g = new Game(5);
g.start();

does the error have something to do with the window.requestAnimationFrame() does that become the object 'this' would be referring to?

  • 1
    Thanks for including the live snippet, but unfortunately, your title mentions one error, but the snippet throws a different error. Please state clearly what code is throwing the error and what error is being thrown (copy and paste the error). (Re the title: `g` is definitely defined.) – T.J. Crowder Jun 14 '20 at 17:40
  • The error the snippet throws is explained [here](http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callback), if that's what you're running into. – T.J. Crowder Jun 14 '20 at 17:42
  • @T.J.Crowder — They seem to match up to me. "Uncaught TypeError: Cannot read property 'processInput' of undefined" and `this` is expected to be `g` but is `undefined`. The question could be clearer though. – Quentin Jun 14 '20 at 17:42
  • @Quentin - Yeah, I think that's probably what they mean. – T.J. Crowder Jun 14 '20 at 17:47

0 Answers0