Grant
Grant

Reputation: 25

How to check mousemove event in if statement Javascript?

I want to be able to change the buttons (38, 40, 37 and 39) to just the mouse so I don't have to use the arrow keys, since I'm changing this this next part will be pointless, I want to know why when i go down and right, it resets using the function reset(); but when I go left and up it doesn't? It just keeps scrolling

    // 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();
        }
    }

Upvotes: 0

Views: 278

Answers (1)

Hendrik
Hendrik

Reputation: 642

It keeps scrolling for left and up, because it seems that in your if statements you're checking against the same boundaries for both left/right and up/down. For going left you have to check whether hero's x position is smaller than the left boundary (usually 0). For going up you have to check whether hero's y position is smaller than the top boundary (also usually 0).

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

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

Upvotes: 1

Related Questions