嗨,大家好我有這樣的代碼的JavaScript OOP的知名度
class Player {
constructor(posX, posY, spdX, spdY, width, height, image){
// Vytvoření hráče
this.posX = posX;
this.posY = posY;
this.spdX = spdX;
this.spdY = spdY;
this.width = width;
this.height = height;
this.image = image;
}
// Draws player
draw(){
var imageObj = new Image();
imageObj.src = this.image;
ctx.drawImage(imageObj, testPlayer.posX, testPlayer.posY);
}
jump(){
this.move('up');
}
// Move
move(direction){
// Returns false if fails
switch(direction){
case 'right':
this.posX+= this.spdX;
break;
case 'left':
this.posX-= this.spdX;
break;
case 'up':
this.posY-= this.spdY;
break;
case 'down':
this.posY+= this.spdY;
break;
default:
return false;
}
}
}
我在跳法問題。 當我想跳起來,我必須上下,但我怎樣才能做到這一點。 因爲我試圖setTimeout(function(){})
但是在那個函數關鍵字裏面不能看到方法移動。如果我做setTimeout(this.move('down'),500)
它不起作用。那麼有什麼想法?
您好感謝的作品,我將迎來後6分鐘 –
良好的解決。如果你使用babel,或者是針對ES6瀏覽器,我會推薦使用@James Kraus的答案,因爲它更乾淨。 –