我正試圖在畫布中開發迷宮遊戲。我手工繪製了迷宮,如檢查畫布中迷宮遊戲的碰撞
context.moveTo(100,700);
context.lineTo(100,600);
context.lineTo(200,600);
context.lineTo(200,500);
還有更多。我想通過這些線移動物體而不移過這些邊界。
爲此,我曾嘗試下面的代碼:
function right() {
context.clearRect(0, 0, canvas.width, canvas.height);
art();
myImage11.src = "Point.png";
x += 20;
if ((x + 20 < 50)) {
context.clearRect(0, 0, canvas.width, canvas.height);
checkcollision();
if (collision == 1) {
x -= 20;
collision = 0;
}
context.drawImage(myImage11, x, y, 50, 50);
}
我checkcollision功能確實
function checkcollision() {
var myImage11 = context.getImageData(x, y, 10, 10);
var pix = myImage11.data;
for (var i = 0; n = pix.length, i < n; i += 4) {
if (pix[i] == 0) {
collision = 1;
}
}
}
但它不工作。請幫忙解決這個問題! 在此先感謝
什麼是你的'checkcollision'功能嗎? – nils
函數checkcollision(){ \t \t \t var myImage11 = context.getImageData(x,y,50,50); \t \t \t var pix = myImage11.data; \t \t \t爲(VAR I = 0; N = pix.length,I
Andrew
每個像素應該是陣列中的4(RGBA)條目(所以10×10 = 400 ARRAYSIZE),http://www.w3schools.com/tags/tryit.asp? filename = tryhtml5_canvas_getimagedata2 – dwana