2
我正在关于精灵的教程: http://www.williammalone.com/articles/create-html5-canvas-javascript-sprite-animation/ 我有一个精灵现在工作,但我喜欢精灵有更多的一个动画。那些动画应该取决于这个精灵应该具有的特定状态。Javascript和HTML5的画布精灵
我喜欢做的是在旧的苹果游戏空中创造伞兵。举例来说,请参阅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;
}
我怎样才能得到这个精灵有这些额外的动画选项?
感谢
对于迟到的回复,我很抱歉。谢谢Ken,这非常有用。 – Smek