Sto realizzando un gioco su tela JavaScript e ne ho bisognoper rilevare se lo schermo a sfioramento su un dispositivo viene tenuto premuto mentre viene toccato. Ho funzionato per raccogliere un tocco, ma non riesco a capire come continuare a chiamare un metodo mentre è tenuto premuto.
Quindi aggiungo i gestori di eventi
function addTouchEventHandlers()
{
screen.canvas.addEventListener( "touchstart", touchStart);
screen.canvas.addEventListener( "touchend", touchEnd );
}
Ho tocco iniziale e tocco finale
function touchStart(e) {
if (gameStarted) {
// Prevent players from inadvertently
// dragging the game canvas
e.preventDefault();
}};
function touchEnd(e) {
var x = e.changedTouches[0].pageX;
var y = e.changedTouches[0].pageY;
if (gameStarted) {
if (x > screen.width/2)
{
processRightTap();
}
else if (x < screen.width/2 && y < screen.height/2)
{
processTopLeftTap(); <- I want to keep calling this method when held down
}
else if (x < screen.width/2 && y > screen.height/2)
{
processBottomLeftTap(); <- I want to keep calling this method when held down
}
// Prevent players from double
// tapping to zoom into the canvas
e.preventDefault();
}
};
I metodi cambiano semplicemente la posizione dei miei sprite
function processBottomLeftTap() {
ship.y += 4;
}
function processTopLeftTap() {
ship.y -= 4;
}
È qualcosa di simile al metodo della tastiera isPressed e isDown? o come creerei un ciclo while attorno al metodo? qualcosa come questo.
while(The screen is being touched at X position)
{
processTopLeftTap();
}
risposte:
1 per risposta № 1Potresti usare setInterval()
nel tuo touchStart
con processTopLeftTap()
nel callback, quindi chiama clearInterval()
sopra touchEnd
.