如何在if语句Javascript中检查mousemove事件?
我希望能够将按钮(38、40、37和39)更改为鼠标,这样我就不必使用箭头键,因为我正在更改此项,下一部分将毫无意义,我想知道为什么当我向下和向右移动时,它会使用函数reset()重置;但当我向左上走的时候,它不会吗?它只是不断滚动如何在if语句Javascript中检查mousemove事件?,javascript,events,mousemove,Javascript,Events,Mousemove,我希望能够将按钮(38、40、37和39)更改为鼠标,这样我就不必使用箭头键,因为我正在更改此项,下一部分将毫无意义,我想知道为什么当我向下和向右移动时,它会使用函数reset()重置;但当我向左上走的时候,它不会吗?它只是不断滚动 // Update game objects var update = function (modifier) { if (38 in keysDown) { // Player holding up hero.y -= hero.s
// 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();
}
}
它一直在向左和向上滚动,因为在if语句中,您似乎在检查左/右和向上/向下的相同边界。要向左走,您必须检查英雄的x位置是否小于左边界(通常为0)。对于上升,您必须检查英雄的y位置是否小于顶部边界(通常为0)
//检查英雄是否超出顶部边界
if(hero.y<0){
重置();
}
//检查英雄是否超出左边界
如果(英雄x<0){
重置();
}
是的,是@ShreyosAdikari
// Check if hero exceeds top boundary
if (hero.y < 0) {
reset();
}
// Check if hero exceeds left boundary
if (hero.x < 0) {
reset();
}