【发布时间】:2017-03-06 09:03:03
【问题描述】:
我正在尝试使用画布在 html/javascript 中制作一个简单的侧滚动回避游戏。在删除(clearRect)移动可变高度剪辑图像时遇到一些麻烦,因此它也不会删除用户在上面控制的精灵/图像,我可以让它删除图像上方/下方的所有内容,但找不到如何准确删除移动阴影图像,因此在制作动画时它不会移除游戏中的英雄!
https://jsfiddle.net/6k354f5x/3/
目前香蕉也被清除,任何帮助将不胜感激!
//<canvas id="board" width="480" height="640"></canvas>
//Getting the canvas
var board = document.getElementById("board");
var cx = board.getContext("2d");
//Example Images
var pipe = new Image();
pipe.src = "http://www.appcycle.me/flappy/img/pipe.png";
var hero = new Image();
hero.src = "http://vignette2.wikia.nocookie.net/drunken-peasants-podcast/images/9/9c/Banana-in-8-bit.png/revision/latest?cb=20150821213530";
//Pipe randomness calculated from board height
var pipeVariation = Math.floor((Math.random() * 250) + 1);
var pipeY = 456;
var pipeX = 350;
//interval
var timer = setInterval(function() {
//draw the hero
cx.drawImage(hero, 0, 150);
//clear the afterimage
cx.clearRect(pipeX, 80, pipe.width / 1.6, pipe.height / 1.6);
//move it on the X-axis some px
pipeX -= 2;
//draw the clipped pipe with some Y-axis placement variation from pipeVariation variable
cx.drawImage(pipe,
0, -pipeY+pipeVariation, pipe.width, pipe.height,
pipeX, 80, pipe.width / 1.6, pipe.height / 1.6)
//Temporary to keep pipe from running away while testing
if (pipeX <= 0) {
clearInterval(timer);
}
});
【问题讨论】:
标签: javascript html canvas