Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript: Challenge Rendering the Game startGame() Method Solution

Issue with the drawHTMLToken()

IHello guys, I get the following error:

Uncaught TypeError: Cannot read property 'drawHTMLToken' of undefined at Game.startGame (Game.js:35) at HTMLButtonElement.<anonymous> (app.js:10) startGame @ Game.js:35 (anonymous) @ app.js:10

My code in Game.js:

class Game {

constructor() {
  this.board = new Board();

  this.players = this.createPlayers();

  this.ready = false;
}

/*
* This getter method should return the Player object whose active property is      * equal to true.
* @return {object}  player - the active player
*/
activePlayer(){
  return this.players.find(player => player.active);
}

createPlayers(){
/** 
* Creates two player objects
* @return  {Array}    An array of two Player objects.
*/
  const players = [new Player('Player 1', 1,'#e15258', true),
                   new Player('Player 2', 2, '#e59a13')];
  return players;
}



/*
* Gets game ready for play
* Initializes game
*/

startGame(){
  this.board.drawHTMLBoard();
  this.activePlayer.activeToken.drawHTMLToken();
  this.ready = true;
}

}

My code in app.js:

const game = new Game();

/**

  • Listens for click on #begin-game and calls startGame() on game object */ //Ashley way

document.getElementById('begin-game').addEventListener('click', function(){ this.style.display = 'none';

document.getElementById('play-area').style.opacity = '1';

game.startGame(); });

I wasn't able to solve this last problem :( Any idea about what is wrong with my code? Many thank!

Rupertson Espinosa
Rupertson Espinosa
4,128 Points

in your Game.js, you forgot to put declare the activePlayer() method as a getter method but typing "get" in front of it.

1 Answer

Ben Antino
Ben Antino
15,699 Points

When you write:

startGame(){ // inside this method // This statment console.log( this.activePlayer.activeToken ); {

Can you see any value in the console of the browser?