- This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
Viewing 2 posts - 1 through 2 (of 2 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › Jquery Canvas Help
Hey,
I am making html5 game using canvas, it has a player which moves using keyboard. This is the code for that :
function onKeyUp(e) {
if (e.keyCode == 39 && player.direction != 'left') {
player.direction = 'right';
} else if (e.keyCode == 37 && player.direction != 'right') {
player.direction = 'left';
} else if (e.keyCode == 38 && player.direction != 'down') {
player.direction = 'up';
} else if (e.keyCode == 40 && player.direction != 'up') {
palyer.direction = 'down';
}
}
Now I want the player to move using virtual arrows that I added using Bootstrap Glyphicon, so I used this code but not working, here’s the code:
$('#up').click(function(){
if (player.direction != 'down'){player.direction = 'up'}
})
I even tried this:
$('#canvas').bind('click',function(){
if ($('#up').click() && player.direction != 'down'){player.direction = 'up'}
})
But what happens that the player moves up immediately when clicked on the canvas to start the game and does not move when done using the glyphicon icon.
Any help? Thanks in advance.
Okay, guys it was my mistake, i did not added the id in my span element.
This worked for me:
$('#board').bind('click',function(){
$("#up").click(function() {
if (player.direction != 'down') {
player.direction = 'up';
}