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