Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialVitaly Khe
7,160 Pointsmy moves do not work as expected..
Behavior: Right key - it jumps to somwhere for more than 76px Left key - it moves 76px left, but 'if' conditions ignored?
class Game {
constructor() {
this.players = this.createPlayers();
this.board = new Board();
this.ready = false;
}
startGame() {
console.log('creating the new board object: const board = new Board()');
this.board.drawHTMLBoard();
console.log('a board has created\n Drawing HTML tokens...');
this.activePlayer.activeToken.drawTHMLToken();
console.log('HTML tokens have been created');
this.ready = true;
}
// createPlayers returns an array of Player {object} instance
createPlayers() {
const player1 = new Player('Player 1', 1, '#e15258', true);
const player2 = new Player('Player 2', 2, '#e59a13');
return [player1, player2];
}
get activePlayer() {
return this.players.find(player => player.isActive);
}
handleKeyDown(event) {
if (this.ready) {
switch(event.key) {
case "ArrowLeft": {
console.log("Left");
this.activePlayer.activeToken.moveLeft();
break;
}
case "ArrowRight": {
console.log("Right");
this.activePlayer.activeToken.moveRight(this.board.columns);
break;
}
case "ArrowDown": {
console.log("Down");
this.activePlayer.activeToken.moveDown();
break;
}
}
}
}
}
class Token {
constructor(index, owner){
this.owner = owner;
this.id = `token-${index}-${owner.id}`;
this.dropped = false;
this.columnLocation = 0;
}
drawTHMLToken() {
let token = document.createElement('div');
document.getElementById('game-board-underlay').appendChild(token);
token.setAttribute('id', this.id);
token.setAttribute('class', 'token');
token.style.backgroundColor = this.owner.color;
}
get htmlToken () {
return document.getElementById(this.id);
}
get offsetLeft() {
return this.htmlToken.offsetLeft;
}
moveLeft() {
if ( this.columnLocation > 0 ) {
this.htmlToken.style.left = this.offsetLeft - 76;
this.columnLocation -= 1;
}
}
moveRight(columns) {
if ( this.columnLocation < columns) {
this.htmlToken.style.right = this.offsetLeft + 76;
this.columnLocation += 1;
}
}
moveDown() {
}
}
2 Answers
Axel Perossa
13,930 PointsAre you using Firefox? I am, and found a bug it seems.. the code doesn't work as it should. It's something with the offset property. By default it should be 0, and it's 16. Right arrow moves the token more than it should, and left arrow moves it back a bit less than it should.
I found this bug report, it says the offset properties are wrongly calculated when the parent isn't statically positioned. In this code, the token's parent is the div #game-board-underlay, and it has 'position: relative'. I tried removing this rule and the token moves correctly, so I think this is it.
Ashley Boucher could you check this?
Axel Perossa
13,930 PointsIn case anyone is thinking about this, I fixed it by removing the left border of game-board-underlay and adding a left offset of 16px to the token class instead.
Zimri Leijen
11,835 PointsmoveRight(columns) {
if ( this.columnLocation < columns) {
this.htmlToken.style.right = this.offsetLeft + 76;
this.columnLocation += 1;
}
}
i believe this is causing the problem. You're using this.htmlToken.style.right
where it should be left.
Vitaly Khe
7,160 PointsVitaly Khe
7,160 PointsAdded following for debugging:
After Right key pressed ONCE the output is following: