さて、私は JavaScript でゲームを開発しています。ゲームのすべての部分をさまざまな JavaScript ファイルにまとめました。したがって、これは Player.js ファイルです。これをブラウザーで実行するたびに (もちろん html ファイルから実行します)、Player オブジェクトが画像から透明な四角形にちらつくという問題が発生します。コードは次のとおりです。
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