【发布时间】:2015-12-14 04:36:52
【问题描述】:
如何在按键上为角色设置动画?我更改了显示下一张图片的精灵位置,但是如何循环播放两张图片,以便在按下键时显示播放器正在运行。
我需要第一帧和第二帧。
关键事件:
if(keys[39]){
//right arrow
if (mario.velX < mario.speed){
mario.velX++;
if(!mario.jumping){
//mario sprite position
mario.frame = 0;
}
}
}
还有绘图功能
this.frame = 0;
var marioImg; //mario image
var that = this;
this.init = function() {
marioSprite = new Image();
marioSprite.src = 'images/mario-sprites.png';
}
this.draw = function(){
that.sX = that.width * that.frame;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(marioSprite, that.sX, that.sY, that.width, that.height, that.x, that.y, that.width, that.height);
}
【问题讨论】:
标签: javascript html canvas sprite