gpt4 book ai didi

javascript - 如何在 html5 中使用键盘箭头键在 Canvas 上移动图像

转载 作者:太空宇宙 更新时间:2023-11-04 13:24:17 25 4
gpt4 key购买 nike

我正在用这段代码在 Canvas 上绘制图像现在它成功地在 Canvas 上绘制了图像我想在 Canvas 上移动图像为此我编写代码我检查是否按下键盘的右键如果按下左键我将增加图像的 x 坐标我将减少 x 坐标,但图像不会在 Canvas 上移动

player = new Image();
player.src = "game_character.png";
context.drawImage(player,player.x * wallDim + wallDim ,player.y * wallDim + wallDim ,50,50);

如何在 Canvas 上移动图像

 var handleInput = function(event, keyState) {
switch(event.which) {
case 37: { // Left Arrow
keyDown.arrowLeft = keyState;
break;
}
case 38: { // Up Arrow
keyDown.arrowUp = keyState;
break;
}
case 39: { // Right Arrow
keyDown.arrowRight = keyState;
break;
}
case 40: { // Down Arrow
keyDown.arrowDown = keyState;
break;
}
}
}

/**
* physics
*
* This function contains the basic logic for the maze.
*/
var physics = function() {
console.log("physics ");
console.log("first condition "+keyDown.arrowRight +player.x+1);
if(keyDown.arrowLeft && player.x-1 >= 0 && map[player.y][player.x-1] != 1) {
player.x--;
redraw = true;
}

if(keyDown.arrowUp && player.y-1 >= 0 && map[player.y-1][player.x] != 1) {
player.y--;
redraw = true;
}

if(keyDown.arrowRight && player.x+1 < map[0].length && map[player.y][player.x+1] != 1) {
console.log("arrow right");
player.x++;
redraw = true;
}

if(keyDown.arrowDown && player.y+1 < map.length && map[player.y+1][player.x] != 1) {
player.y++;
redraw = true;
}
if(keyDown.arrowRight && player.x+1 >= map[0].length)
{
player.x++;
document.getElementById("canvas_div").style.display="none";
document.getElementById("end_screen_div").style.display="block";
//alert("completed");
}
}

/**
* draw
*
* This function simply draws the current state of the game.
*/
var draw = function() {

// Don't redraw if nothing has changed
if(!redraw)
return;

context.clearRect(0, 0, cols, rows);
context.beginPath();

// Draw the maze
for(var a = 0; a < rows; a++) {
for(var b = 0; b < cols; b++) {
switch(map[a][b]) {
case C.EMPTY: context.fillStyle = colors.empty; break;
case C.WALL: context.fillStyle = colors.wall; break;
}

context.fillRect(b * wallDim, a * wallDim, wallDim, wallDim); // x, y, width, height
}
}

// Draw the player
/* context.fillStyle = colors.player;
context.arc(
player.x * wallDim + wallDim / 2, // x position
player.y * wallDim + wallDim / 2, // y position
wallDim / 2, // Radius
0, // Starting angle
Math.PI * 2, // Ending angle
true // antiClockwise
);*/


player = new Image();
player.src = "game_character.png";

context.drawImage(player,player.x * wallDim + wallDim ,player.y * wallDim + wallDim ,50,50);

var firstplayer=new Image();
firstplayer.src="top_character01.png";

context.drawImage(firstplayer,680,0,60,60);

var secondplayer= new Image();
secondplayer.src="top_character02.png";

context.drawImage(secondplayer,750,0,60,60);

context.fill();
context.closePath();

redraw = false;
}

最佳答案

在你的 draw 方法中,你每次都重新初始化播放器:

player = new Image();
player.src = "game_character.png";

因此您删除了由您的事件处理程序修改的 player.x。

您应该只在绘制函数之外初始化播放器一次。您可以像这样移动初始化:

var player = new Image();
player.src = "game_character.png";
var draw = function() {

绝对不需要在绘图函数中调用player.src = "game_character.png";

作为一般规则,在处理动画时,尽量从绘图函数中删除所有可能的内容,这应该尽可能快。

关于javascript - 如何在 html5 中使用键盘箭头键在 Canvas 上移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11344875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com