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” or “right”.
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");
}
}
I have altered the code as follows.
$('#left').mousedown(function(e) {
var nextpos = parseInt($("#player").css("left"))-5;
if(nextpos > 0){
$("#player").css("left", ""+nextpos+"px");
}
});
Now, the problem I am having is that, when I click on the “left” or “right” text, it doesn’t make the spaceship move smoothly. With the original code, if I keypressed, the spaceship would move a little, and if I held the key down it would move smoothly across.
How can I adjust the code so that when I click on the “left” or “right” it moves a touch in either direction (smoothly), or if I hold the mouse down on “left” or “right” it continuously moves the spaceship, rather than what it is doing at the moment.
Play with code here: Edit this Fiddle - jsFiddle - Online Editor for the Web (JavaScript, MooTools, jQuery, Prototype, YUI, Glow and Dojo, HTML, CSS)
Thanks in advance for the help!