JavaScript流程设备触摸屏被按下

时间:2017-05-06 23:20:28

标签: javascript android ios device touchscreen

我正在制作一个JavaScript画布游戏,我需要检测设备上的touchScreen是否被按下,因为它被点击了。我已经努力尝试接触但我无法弄清楚如何在按下时调用方法。

所以我添加了活动handelers

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();       
}   
};

方法只是改变我的精灵位置

function processBottomLeftTap() {      
 ship.y += 4;
}

function processTopLeftTap() {      
ship.y -= 4;
}

他们的东西类似于键盘方法isPressed和isDown吗? 或者我如何在方法周围创建一个while循环? 类似的东西。

while(The screen is being touched at X position)
{
processTopLeftTap();
}

1 个答案:

答案 0 :(得分:1)

您可以在private static IEnumerable<Item> GetSimilarItems(int days, string type, float counterOne, float counterTwo) { var similarSubItems = new HashSet<string>(); var c9 = counterOne * 0.9; var c1 = counterOne * 1.1; if (days > 180) { foreach (var p in SubItemCache.Values) if (p.CounterOne >= c9 && p.CounterOne <= c1) similarSubItems.Add(p.ID); } else { foreach (var p in SubItemCache.Values) if (p.CounterTwo >= c9 && p.CounterTwo <= c1) similarSubItems.Add(p.ID); } var days0 = days - 5; var days1 = days + 5; foreach (var p in ItemCache.Values) if (p.days >= days0 && p.days <= days1 && p.Type == type && similarSubItems.Contains(p.Key)) yield return p; } 中使用setInterval()并在回调中使用touchStart,然后在processTopLeftTap()上致电clearInterval()