我正在关注关于精灵的教程:http: //www.williammalone.com/articles/create-html5-canvas-javascript-sprite-animation/ 我现在有一个精灵在工作,但我喜欢精灵有多个动画. 这些动画应该取决于这个精灵应该有的特定状态。
我喜欢做的是在旧的 Apple 游戏空降中创建伞兵。有关示例,请参见http://www.youtube.com/watch?v=pYujWCNUuNw 您会看到那些士兵从直升机上掉了下来。当他们在地上时,他们会闲置一会儿,然后他们开始时不时地走路。
这是我的精灵方法:
function sprite(options) {
var that = {},
frameIndex = 0,
tickCount = 0, ticksPerFrame = options.ticksPerFrame || 0, numberOfFrames = options.numberOfFrames || 1;
that.x = options.x, that.y = options.y,
that.context = options.context;
that.width = options.width;
that.height = options.height;
that.image = options.image;
that.update = function() {
tickCount += 1;
if (tickCount > ticksPerFrame) {
tickCount = 0;
// If the current frame index is in range
if (frameIndex < numberOfFrames - 1) {
// Go to the next frame
frameIndex += 1;
} else {
frameIndex = 0;
}
}
};
that.render = function() {
// Draw the animation
that.context.drawImage(that.image,
frameIndex * that.width / numberOfFrames,
0,
that.width / numberOfFrames,
that.height, that.x,
that.y,
that.width / numberOfFrames,
that.height);
};
return that;
}
我怎样才能让这个精灵拥有那些额外的动画选项?
谢谢