Skip to content
Browse files

fix menu for touch device

  • Loading branch information...
1 parent 9ea3dbe commit dd6f5dffaf4b61888a6b01e45c81faf0503ece70 @digitaltom committed
Showing with 23 additions and 14 deletions.
  1. +21 −11 js/controls.js
  2. +1 −2 js/game.js
  3. +1 −1 js/menu.js
View
32 js/controls.js
@@ -2,12 +2,31 @@
function hideControls() {
document.getElementById("pad-controls").style.visibility = "hidden"
+ enable_default_touch()
}
function is_touch_device() {
return !!('ontouchstart' in window);
}
+function preventDefault(event) {
+ event.preventDefault();
+}
+
+// prevent scrolling etc
+function disable_default_touch() {
+ document.body.addEventListener('touchmove', preventDefault, false);
+ document.body.addEventListener('touchstart', preventDefault, false);
+ document.body.addEventListener('touchend', preventDefault, false);
+}
+
+// re-enable touch events for menu
+function enable_default_touch() {
+ document.body.removeEventListener('touchmove', preventDefault, false);
+ document.body.removeEventListener('touchstart', preventDefault, false);
+ document.body.removeEventListener('touchend', preventDefault, false);
+}
+
function registerControls() {
window.onkeydown = function (e) {
@@ -67,18 +86,9 @@ function registerControls() {
document.getElementById("pad-controls").style.visibility = "visible"
}
- // prevent scrolling
- document.body.addEventListener('touchmove', function (event) {
- event.preventDefault();
- }, false);
+ // prevent scrolling etc
+ disable_default_touch()
- document.body.addEventListener('touchstart', function (event) {
- event.preventDefault();
- }, false);
-
- document.body.addEventListener('touchend', function (event) {
- event.preventDefault();
- }, false);
var left = document.getElementById("control-left");
left.addEventListener('touchstart', function (event) {
View
3 js/game.js
@@ -457,11 +457,10 @@ function gameLoop() {
function initGame() {
-
window.clearInterval(gameInterval);
var canvas = document.getElementById("game");
ctx = canvas.getContext("2d");
-
+ hideMenus()
hideControls()
// draw initial level for menu background
load_level()
View
2 js/menu.js
@@ -46,6 +46,7 @@ if (button = document.getElementById("button-play3")) {
var start_menu = document.getElementById("game-menu");
function showStartMenu() {
+ hideControls()
start_menu.style.visibility = "visible";
}
@@ -61,7 +62,6 @@ document.getElementById("button-restart").addEventListener('click', function (ev
}, false);
document.getElementById("button-menu").addEventListener('click', function (event) {
- hideGameOver()
initGame()
showStartMenu()
}, false);

0 comments on commit dd6f5df

Please sign in to comment.
Something went wrong with that request. Please try again.