JavaScriptキャンバスゲームを作成していますが、必要ですデバイスのタッチスクリーンがタップされているのではなく押し続けられているかどうかを検出します。タッチを拾うように動作していますが、押し続けている間、メソッドを呼び出し続ける方法がわかりません。
だから私はイベントハンデラーを追加します
function addTouchEventHandlers()
{
screen.canvas.addEventListener( "touchstart", touchStart);
screen.canvas.addEventListener( "touchend", touchEnd );
}
タッチスタートとタッチエンドがあります
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();
}
};
メソッドは単に私のスプライトのy位置を変更します
function processBottomLeftTap() {
ship.y += 4;
}
function processTopLeftTap() {
ship.y -= 4;
}
それらはキーボードメソッドisPressedとisDownに似ていますか? または、メソッドの周りにwhileループを作成するにはどうすればよいですか? このようなもの。
while(The screen is being touched at X position)
{
processTopLeftTap();
}
回答:
回答№1は1あなたは使うことができます setInterval()
あなたの touchStart
〜と processTopLeftTap()
コールバックで、次に呼び出します clearInterval()
に touchEnd
.