好的,所以我正在用JavaScript開發一個遊戲。我用不同的JavaScript文件組織了遊戲的所有部分。所以,這是Player.js文件,每當我在瀏覽器中運行它(當然是從html文件運行)時,我都會遇到這個問題:Player對象從圖像閃爍到透明矩形:繼承代碼:在JavaScript中正確顯示幀
function Player() {
this.frames = [];
this.right = true;
this.currentFrame = 0;
this.currentAction = "WALKING";
this.image = new Image();
this.x = 0;
this.y = 0;
this.setPosition = function(x, y) {
this.x = x;
this.y = y;
};
this.setVector = function(x, y) {
this.x += x;
this.y += y;
};
this.setAction = function(action) {
this.currentAction = action;
};
this.setRight = function(bool) {
this.right = bool;
}
this.draw = function(context) {
if(this.right == true) {
if(this.currentAction == "WALKING") {
this.frames = [ "res/playerRight.png" ];
}
} else if(this.right == false) {
if(this.currentAction == "WALKING") {
this.frames = [ "res/playerLeft.png" ];
}
}
if(this.currentFrame < this.frames.length) {
this.currentFrame += 1;
this.image.src = this.frames[this.currentFrame - 1];
context.drawImage(this.image, this.x,
this.y, 32, 32);
} else {
this.currentFrame = 0;
}
};
}
繼承人是做什麼的一些圖片: http://i.stack.imgur.com/1RcOC.png http://i.stack.imgur.com/fxbNY.png
這可能是因爲圖像加載時沒有準備好。 – Ibu
圖像被加載,如果對象加載圖像本身,圖像被自動加載,導致主JavaScript文件調用對象。如果是在圖像未加載的情況下,則圖像根本不會顯示。 – UnhandyFir9
@ UnhandyFir9:如果圖片沒有加載,瀏覽器會下載圖片,然後顯示它,這就是爲什麼你會看到閃爍 - 瀏覽器需要繪製東西,但它仍然在等待圖像文件從網絡到達。 – slebetman