I'm trying to port a game on Android and in the game the player press space to jump and I can't find any working solution to simulate a spacebar event when a user touch the screen
Here's the code I've found until now :
/**
* Keep track of the spacebar events
*/
var KEY_CODES = {
32: 'space'
};
var KEY_STATUS = {};
for (var code in KEY_CODES) {
if (KEY_CODES.hasOwnProperty(code)) {
KEY_STATUS[KEY_CODES[code]] = false;
}
}
document.onkeydown = function(e) {
var keyCode = (e.keyCode) ? e.keyCode : e.charCode;
if (KEY_CODES[keyCode]) {
e.preventDefault();
KEY_STATUS[KEY_CODES[keyCode]] = true;
}
};
document.onkeyup = function(e) {
var keyCode = (e.keyCode) ? e.keyCode : e.charCode;
if (KEY_CODES[keyCode]) {
e.preventDefault();
KEY_STATUS[KEY_CODES[keyCode]] = false;
}
};
document.addEventListener("touchstart", function(e) {
document.onkeydown({ keyCode: 32 });
});
document.addEventListener("touchend", function(e) {
document.onkeyup({ keyCode: 32 });
});
I don't understand why this doesn't work...
And then this :
// jump higher if the space bar is continually pressed
if (KEY_STATUS.space && jumpCounter) {
player.dy = player.jumpDy;
}
Above is all the code that use the space bar event