我有一个 800 x 100 的背景图像,我试图从中提取部分(如精灵表)以生成背景,因为我的印象是这是一种有效的方式(也似乎是我可以在未来玩弄并学习生成背景!)
但是它不起作用,在他的第一个动画帧中(我发现这是因为放慢了它)它显示了整个背景图像的一半(不是我想要使用的部分)然后它将它向下移动大概 800 像素并显示它和到第三帧它就消失了!
请帮助:/谢谢!
var bricks = [0, 1, 1, 1, 1, 2];
function createBackground() {
for(var i = 0; i < bricks.length; i++) {
drawBackground(bricks[i]);
}
}
var bg = new Image();
bg.src = 'bgsheet2.png';
var srcX, srcY = 0,srcW = 100,srcH = 100,destX = 0,destY = canvas.height-100,destW = 100,destH = 100;
function drawBackground(type) {
switch(type) {
case 1:
srcX = 0;
ctx.drawImage(bg,srcX,srcY,srcW,srcH,destX,destY,destW,destH);
destX+=100;
break;
case 2:
srcX = 100;
ctx.drawImage(bg,srcX,srcY,srcW,srcH,destX,destY,destW,destH);
destX+=100;
break;
case 3:
srcX = 200;
destX+=100;
ctx.drawImage(bg,srcX,srcY,srcW,srcH,destX,destY,destW,destH);
break;
default:
srcX = 300;
ctx.drawImage(bg,srcX,srcY,srcW,srcH,destX,destY,destW,destH);
destX+=100;
break;
}
}
//this is in my main animation loop
createBackground();