這個週末試圖讓我的第一場比賽。我最終讓我的矩形與我的地圖同時出現。我的矩形可以移動!但只要我點擊一個按鈕來移動它,地圖就會消失。這是我需要做出某種遊戲循環來持續更新每個按鈕點擊地圖或設置一個計時器嗎?或者我還有其他類型的錯誤?謝謝大家!爲什麼我的地圖每當我的精靈移動時都會消失?
var canvas = <HTMLCanvasElement>document.getElementById('myCanvas');
var context = canvas.getContext("2d");
var img = new Image();
img.onload = function() {
context.drawImage(img, 0, 0);
}
img.src = "";
var mapArray =
["############################",
"# # # o ##",
"# #",
"# #### ##### ## #",
"## # # ## #",
"### ## # #",
"# ### # #",
"# #### ### #",
"# ## # o #",
"# o # # o ### ### #",
"# # # #",
"############################"];
//need to add wall.scource = and grass.source =
var wall = new Image();
var grass = new Image();
grass.src = "http://vignette2.wikia.nocookie.net/tibia/images/6/60/Grass_(Tile).gif/revision/latest?cb=20080817072800&path-prefix=en";
wall.src = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJyofLT0tMSw3QTo6LC87RD84Nzc5OjcBCgoKBQUFDgUFDisZExkrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrK//AABEIACAAIAMBIgACEQEDEQH/xAAYAAADAQEAAAAAAAAAAAAAAAACAwQFAP/EACkQAAEDAwIDCQEAAAAAAAAAAAECAxEABCESEyMxURQyM0Fhc4GT0QX/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEQMRAD8Au/oXDzT2ltZSJxAHKp27242yC5nPxQ3Qm5dVqzOMelI7vNMdYoKm718Jw4c85ApN5cXIbWvdUTtk48qBKkGcgEdaFRT2e4AIPCVQaF+1tvuawAVEQKlVBM8ulHfPoXeunfaKQqJ1ilrW0UnjNfYP2gVoCoURIpdwhoMuhKTG2ZNODiJI3miPcFddvMm1cSHGvDIEKBJoP//Z";
var posX = 0;
var posY = 0;
//for loops set images at given coordinates according to position on mapArray
for (var y = 0; y < mapArray.length; y++) {
for (var x = 0; x < mapArray[0].length; x++) {
if (mapArray[y][x] == "") {
context.drawImage(grass, (32 * x), (32 * y), 32, 32)//last two are size of image)
}
if (mapArray[y][x] == "#") {
context.drawImage(wall, (32 * x), (32 * y), 32, 32)//last two are size of image)
}
}
}
context.rect(posX, posY, 32, 32)
context.stroke();//traces path, might not need this
//moves character
function move(e) {//next five lines are newly added. Final line of function is drawimage function that is new as well
var ctx = canvas.getContext('2d');// create backing canvas
var backCanvas = document.createElement('canvas');
backCanvas.width = canvas.width;
backCanvas.height = canvas.height;
var backCtx = backCanvas.getContext('2d');
//alert(e.keyCode);//gives feedback to what each keyCode is
if (e.keyCode == 39) {
posX += 5;
}
if (e.keyCode == 37) {
posX -= 5;
}
if (e.keyCode == 40) {
posY += 5;
}
if (e.keyCode == 38) {
posY -= 5;
}
canvas.width = canvas.width;//clears the board after each move
context.rect(posX, posY, 32, 32)
context.stroke();
ctx.drawImage(backCanvas, 0, 0);
}
document.onkeydown = move;
謝謝,但我不能讓任何工作。第一種解決方案沒有解決問題,第二種解決方案有一些Visual Studio不喜歡的類型錯誤,並禁用了矩形的移動能力。 – ScottVMeyers
第一次修復你的意思是每次運行你的mapArray代碼?您在Visual Studio中遇到了哪些類型的錯誤?如果它禁用了矩形的移動,它聽起來像是在矩形移動之前Javascript中的移動函數中斷了,您是否檢查了瀏覽器的控制檯是否有錯誤?在您的備份畫布中,您在第一次渲染矩形之前保存了它的圖像,對嗎? – andrrs
我只是在使用備份畫布。我只是無法讓它工作。我不認爲我知道代碼的哪個部分去了哪裏。我已經把它放在了我的移動函數的頂部,我把一些放在頂部,一些放在底部。我試圖在頁面開始處聲明一些變量。我無法完成任何工作,每次我仍然失去我的圍牆。 – ScottVMeyers