【发布时间】:2014-06-13 11:05:31
【问题描述】:
已编辑:以下渲染精灵表图像;现在我需要为每个实体赋予不同的行为:男孩的关键动作,猫头鹰的随机动画。我可以用孤立的图像来做到这一点,但是在哪里/如何包含每个 drawImage 实例的行为?谢谢。
<script>
var width = 50,
height = 50,
frames = 3,
currentFrame = 0,
canvas = document.getElementById("myCanvas");
ctx = canvas.getContext("2d");
image = new Image();
image.src = "sprites.png"
var draw = function(){
ctx.clearRect(200, 400, width, height); //boy
ctx.clearRect(150, 300, width, height); //owl
ctx.clearRect(50, 400, width, height); //goat
ctx.drawImage(image, 0, height * currentFrame, width, height, 200,400, width, height); //boy
ctx.drawImage(image, 50, 50 * 2, width, height, 150, 300, width, height); //owl
ctx.drawImage(image, 150, 50 * currentFrame, width, height, 50, 400, width, height); //goat
ctx.drawImage(image, 100, 50 * 3, width, height, 150, 400, width, height); //bush
if (currentFrame == frames) {
currentFrame = 0;
} else {
currentFrame++;
}
}
setInterval(draw, 550);
【问题讨论】:
-
谢谢——tfm 很有帮助。
标签: html canvas html5-canvas sprite sprite-sheet