好的,所以我正在用 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