【问题标题】:HTML5 canvas and javascript move player with mouse movementHTML5 画布和 javascript 通过鼠标移动移动播放器
【发布时间】:2015-04-24 16:19:14
【问题描述】:

到目前为止,我可以使用箭头键移动玩家,但我一直在尝试弄清楚如何通过鼠标移动来移动玩家,例如向右移动鼠标会增加玩家 X 位置并将鼠标移动到左边会减少 X 位置,但我正在努力实现这一点!

我是如何制作我的播放器对象的

player = {
    x: width / 30,//player properties 
    y: height - 5,
    width: 16,
    height: 18,
    speed: 3,
    velX: 0,
    velY: 0,
    jumping: false,
    grounded: false

},

我的控制功能

 if (keys[38] || keys[32]) {
    // up arrow or space
    if (!player.jumping && player.grounded) {
        player.jumping = true;
        player.grounded = false;
        player.velY = -player.speed * 2;
        var audio = new Audio('jump.mp3');
        audio.play();
    }
}
if (keys[39]) {
//mouseControl = false;
    // right arrow
    if (player.velX < player.speed) {
        player.velX++;

if(count ==2)
 {
 count = 0;
 }
else {
 count++;
}
    }
}
     if (keys[37]) {
   //mouseControl = false;
    // left arrow
    if (player.velX > -player.speed) {
        player.velX--;
        if(count ==2)
  {
   count = 0;

}
 else {
 count++;
 }
    }
}

player.velX *= friction;
player.velY += gravity;

ctx.clearRect(0, 0, width, height);
ctx.fillStyle = "black";
ctx.beginPath();

player.grounded = false;
}

我是如何得到钥匙的

 //if key is pressed down = true
 document.body.addEventListener("keydown", function (e) {
    keys[e.keyCode] = true;
 });
 //if key is not pressed = false
 document.body.addEventListener("keyup", function (e) {
keys[e.keyCode] = false;
 });


 window.addEventListener("load", function () {
game_loop();
 });

【问题讨论】:

    标签: javascript html function canvas html5-canvas


    【解决方案1】:

    您需要添加一个mousemove 事件。并且有两个变量来存储oldXoldY 鼠标位置,以及newXnewY 鼠标位置。然后在每次调用mousemove 时,newXnewY 将是当前鼠标位置,oldXoldY 将是最后一次调用mousemove 的鼠标位置。最后,您只需减去 newX - oldX 即可获得鼠标 X 位置的差异 (diffX)。如果是肯定的,则将鼠标向右移动,如果为负,则将鼠标向左移动。 diffY 也是如此。这是一个例子:

    var oldX = 0, oldY = 0, newX, newY;
    
    canvas.addEventListener("mousemove", function(e) 
    { 
        // Get the current mouse position
        var cRect = canvas.getBoundingClientRect();
        newX = e.clientX - cRect.left;
        newY = e.clientY - cRect.top;
    
        // Subtract from the old mouse position
        var diffX = newX - oldX;     
        var diffY = newY - oldY;         
    
        ctx.clearRect(0,0,canvas.width,canvas.height);
    
        if(diffX > 0)
            player.x += player.speed; // If the mouse moved right, move right
        else
            player.x -= player.speed; // Otherwise move left.
    
        ctx.fillRect(player.x, player.y, 50, 50); // draw player
    
        // Now make the current mouse potion the old mouse position
        oldX = newX;
        oldY = newY
    });
    

    Here is a working fiddle example

    【讨论】:

    • omg 非常感谢你的解释,并给了我我现在理解的代码,只是为了让鼠标左键让玩家跳跃。
    • @user186 您只需将click 事件添加到canvas。并将您的跳转代码放在那里。 Example here.
    • @user186 请注意,如果此答案回答了您的问题,您可以将其标记为“已接受”,即左侧的绿色箭头。
    • 我尝试添加我的跳转代码,它没有跳转嗯,但它运行没有错误
    • @user186 之前跳跃是否只使用箭头键?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    相关资源
    最近更新 更多