3

I am building a Tetris game in javaScript. I am trying to make it playable even on an android phone, so I added four arrow buttons like this:

<button onclick="rightArrow()">→</button>

I have four functions (triggered by keyboard) like this one:

function right () {
 // Move brick to the right 
}

Is there a way to call the right() function again and again when user holds the button until he let it go? I found several similar questions here and elsewhere, but the answers were above my level of javaScript knowledge. I am not using jQuery. My game is here.

webnoob
  • 15,747
  • 13
  • 83
  • 165
Radek John
  • 105
  • 8

3 Answers3

2

I put together a small example of how this can be achieved using the touchstart and touchend events.

let counter = 0
let held = false
let button = document.getElementById('button')

// touch events
button.addEventListener('touchstart', (event) => { held = true })
button.addEventListener('touchend', (event) => { held = false })

// mouse events
button.addEventListener('mousedown', (event) => { held = true })
button.addEventListener('mouseup', (event) => { held = false })

// loop
function animate() {
  document.getElementById('counter').innerHTML = counter
  
  if (held) { counter++ }
  else if (counter > 0) { counter-- }

  window.requestAnimationFrame(animate)
}

animate()
<button id="button">
  Press and hold!
</button>

<div id="counter">
  0
</div>

The key is to set some variable when touchstart happens so we know the user is holding down, until we detect for a touchend event, which signals the user has let go. In a separate loop, we execute code based on this variable. In your case, you could check in your main game loop and then run the move code (be careful that it might execute very frequently; but this can be easily resolved by a timer).

I also added the mousedown and mouseup events, so that this code can work on desktops too, but you can also confirm this works on mobile by opening up device preview mode in Chrome Devtools (which uses touch events when you click).

Michael
  • 375
  • 4
  • 7
1

Here is a simple example:

HTML:
<div id="a"></div>

CSS:
#a {
  width: 200px;
  height: 200px;
  border: 1px solid black;
}

Javascript

const myDiv = document.getElementById('a');
let currentMargin = 0;

function shiftDivRight() {
  currentMargin += 10;
  myDiv.style.marginLeft = currentMargin + 'px';
}
document.addEventListener('keydown', () => {
  shiftDivRight();
});

This will move the div to the right as long as any key is pressed down.

To fire on a specific key for example the enter key:

document.addEventListener('keydown', (event) => {
   if(event.which === 13) {
      shiftDivRight();
   }
});
Brandon
  • 1,747
  • 2
  • 13
  • 22
0
        <script type="text/javascript">
        let i = 0;
        let pressed = false;

        document.getElementById('div-1').onmousedown = ()=> {
            pressed = true;
            increment();
        }

        document.getElementById('div-1').onmouseup = ()=> {
            pressed = false;
            increment();
        }

        function increment(){
            
            if(!pressed) return;
            else i++;
            console.log(i);
            setTimeout(()=> increment(), 10)
        }
    </script>