我试图通过在矩形的x / y位置加1来单击按钮时移动矩形,但速度变量一直说未定义。
// . . . //
function component(width, height, color, x, y) {
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.update = function(){
ctx = gameArea.context;
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
this.newPos = function(){
this.x += this.speedX;
this.y += this.speedY;
}
}
function updateGameArea(){// Update games at 50fps
gameArea.clear();
bluePiece.newPos();
bluePiece.update();
}
function moveUp(piece){
piece.speedY -= 1;
console.log("UP "+piece+" S:"+piece.speedY);
}
function moveDown(piece){
piece.speedY += 1;
console.log("DOWN "+piece+" S:"+piece.speedY);
}
function moveLeft(piece){
piece.speedX -= 1;
console.log("LEFT "+piece+" S:"+piece.speedX);
}
function moveRight(piece){
piece.speedY += 1;
console.log("RIGHT "+piece+" S:"+piece.speedX);
}
注意:如果您希望查看完整代码以更好地了解正在发生的情况,请转到here并按ctrl + U查看完整代码。
答案 0 :(得分:1)
您正在将一个字符串传递给该函数,之后访问String的属性,这肯定是未定义的。
试试console.log(typeof piece);