Add touch control for mobile devices

This commit is contained in:
Marcel
2018-01-18 12:00:25 +01:00
parent b77baa06f7
commit 69a441189f
3 changed files with 27 additions and 1 deletions

View File

@@ -36,8 +36,10 @@
<a id="git-repo" href="https://github.com/KingOfDog/Tetris.js" target="_blank">Github</a>
</div>
</div>
<script src="https://hammerjs.github.io/dist/hammer.js"></script>
<script src="language.js"></script>
<script src="tetris.js"></script>
<script src="menu.js"></script>
<script src="touch-control.js"></script>
</body>
</html>

View File

@@ -308,7 +308,7 @@ document.addEventListener('keydown', event => {
});
function startGame() {
arena = createMatrix(fieldSize.x, fieldSize.y)
arena = createMatrix(fieldSize.x, fieldSize.y);
playerReset();
update();
updateScore();

24
touch-control.js Normal file
View File

@@ -0,0 +1,24 @@
const hammertime = new Hammer(document.getElementById("tetris"));
hammertime.on('swipeleft', () => {
keys.left.action();
});
hammertime.on('swiperight', () => {
keys.right.action();
});
hammertime.on('swipe', (e) => {
console.log(e.direction);
keys.down.action();
});
hammertime.on('tap', (e) => {
if (e.tapCount >= 2) {
keys.rotateRight.action();
}
});
hammertime.on('press', () => {
keys.down.action();
});