【问题标题】:How to check mousemove event in if statement Javascript?如何在if语句Javascript中检查mousemove事件?
【发布时间】:2013-05-29 08:06:02
【问题描述】:

我希望能够将按钮(38、40、37 和 39)更改为鼠标,这样我就不必使用箭头键,因为我要更改它,下一部分将毫无意义,我想知道为什么当我向下和向右时,它会使用函数 reset() 重置;但是当我向左和向上时它不会?它只是不断滚动

    // Update game objects
var update = function (modifier) {
    if (38 in keysDown) { // Player holding up
        hero.y -= hero.speed * modifier;
        if(hero.y > canvas.height){
            reset();
        }
    }
    if (40 in keysDown) { // Player holding down
        hero.y += hero.speed * modifier;
        if(hero.y > canvas.height){
            reset();
        }
    }
    if (37 in keysDown) { // Player holding left
        hero.x -= hero.speed * modifier;
        if(hero.x > canvas.width){
            reset();
        }
    }
    if (39 in keysDown) { // Player holding right
        hero.x += hero.speed * modifier;
        if(hero.x > canvas.width){
            reset();
        }
    }

【问题讨论】:

  • 是的,它是@ShreyosAdikari

标签: javascript events mousemove


【解决方案1】:

它一直在向左和向上滚动,因为在您的 if 语句中,您似乎在检查左/右和上/下的相同边界。要向左走,您必须检查英雄的 x 位置是否小于左边界(通常为 0)。上升时你必须检查英雄的 y 位置是否小于顶部边界(通常也是 0)。

// Check if hero exceeds top boundary
if (hero.y < 0) {
    reset();
}

// Check if hero exceeds left boundary
if (hero.x < 0) {
    reset();
}

【讨论】:

    猜你喜欢
    • 2019-03-30
    • 2017-12-08
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 2015-03-13
    • 1970-01-01
    相关资源
    最近更新 更多