Created
June 14, 2014 14:24
-
-
Save d8ta/0de69bb86f6a4b59e3ae to your computer and use it in GitHub Desktop.
Habe die touchsteuerung in die normale reingepackt; var (touchLeft usw.) holen sich die Button aus dem HTML und werden bei touch aktiv; Habe dann versucht in der KeyUp z.b Hammer wieder auf .off zu stellen. Läuft aber nicht
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Spielsteuerung | |
* @returns {movePlayer} | |
*/ | |
function movement() { | |
var up = down = left = right = touchLeft = touchRight = touchDown = touchUp = false; | |
var touchLeft = document.getElementById('left'); | |
Hammer(touchLeft).on("touch", function() { | |
touchLeft = true; | |
}); | |
var touchRight = document.getElementById('right'); | |
Hammer(touchRight).on("touch", function() { | |
touchRight = true; | |
}); | |
var touchUp = document.getElementById('up'); | |
Hammer(touchUp).on("touch", function() { | |
touchUp = true; | |
}); | |
var touchDown = document.getElementById('down'); | |
Hammer(touchDown).on("touch", function() { | |
touchDown = true; | |
}); | |
function keysUp(key) { | |
// links | |
if (key.keyCode == 39 || touchLeft) { | |
left = touchLeft = false; | |
} | |
// rechts | |
if (key.keyCode == 37 || touchRight) { | |
right = touchRight = false; | |
} | |
// runter | |
if (key.keyCode == 40 || touchDown) { | |
down = touchDown = false; | |
} | |
// hoch | |
if (key.keyCode == 38 || touchUp) { | |
up = touchUp = false; | |
} | |
} | |
function keysDown(key) { | |
// links | |
if (key.keyCode == 39 || touchLeft) { | |
left = touchLeft = true; | |
} | |
// rechts | |
if (key.keyCode == 37 || touchRight) { | |
right = touchRight = true; | |
} | |
// runter | |
if (key.keyCode == 40 || touchDown) { | |
down = touchDown = true; | |
} | |
// hoch | |
if (key.keyCode == 38 || touchUp) { | |
up = touchUp = true; | |
} | |
} | |
document.onkeyup = keysUp; | |
document.onkeydown = keysDown; | |
return function movePlayer() { | |
// links | |
if (left || touchLeft) { | |
player.xPos += playerSpeed; | |
} | |
// rechts | |
if (right || touchRight) { | |
player.xPos -= playerSpeed; | |
} | |
// runter | |
if (down || touchDown) { | |
player.yPos += playerSpeed; | |
} | |
// hoch | |
if (up || touchUp) { | |
player.yPos -= playerSpeed; | |
} | |
/** | |
* Setzt Spieler wieder auf maximale Canvaswerte zurück, falls er aus dem Canvas steuert (Wandkollision) | |
*/ | |
if (player.xPos < 0 + player.radius) { | |
player.xPos = 0 + player.radius; | |
} | |
if (player.yPos < 0 + player.radius) { | |
player.yPos = 0 + player.radius; | |
} | |
if (player.xPos > canvas.width - player.radius) { | |
player.xPos = canvas.width - player.radius; | |
} | |
if (player.yPos > canvas.height - player.radius) { | |
player.yPos = canvas.height - player.radius; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PS: keysDown und keysUp nutze ich schon für die Tastatursteuerung, leider bewirkt das false setzten der touchgeschichten in der keysUp nicht das das touch deaktiviert wird, auch das Hammer(touchLeft).off läuft nicht, da er das nicht dort drin haben will.