MartinH23
MartinH23

Reputation: 13

JQuery mousedown issue

What I am trying to achieve is changing the way a player moves in a JQuery game.
Rather than a keypress I am trying to get the player to move if they click/mousedown on text, with id "left".

Original code is as follows.

            if(jQuery.gameQuery.keyTracker[65]){ //this is left! (a)
                var nextpos = parseInt($("#player").css("left"))-5;
                if(nextpos > 0){
                    $("#player").css("left", ""+nextpos+"px");
                }
            }

My code is as follows, and when I play, it just continuously moves the player left without me pressing anything. It shouldn't move left until I click or mousedown on the "left" id text.

            if ($('#left').mousedown){
                var nextpos = parseInt($("#player").css("left"))-5;
                if(nextpos > 0){
                    $("#player").css("left", ""+nextpos+"px");
                }
            }

Upvotes: 0

Views: 336

Answers (1)

mu is too short
mu is too short

Reputation: 434685

This:

if ($('#left').mousedown){

will always be true. You're checking if $() has a mousedown property and it always will because all jQuery objects have a mousedown method. You probably want to bind a mousedown event handler:

$('#left').mousedown(function(e) {
    //...
});

Upvotes: 1

Related Questions