使用保持事件Corona SDK进行多点触控检测

时间:2014-04-05 08:09:56

标签: android lua corona

我有这个大问题,因此我很生气。我不知道如何制作一个处理多个物体的功能。介绍我有什么,应该做什么。 我有3个对象,两个按钮:LEFT和RIGHT。和拍摄按钮。我需要能够在按住旋转按钮的同时进行拍摄。我有这个(下面的代码)它确实有效,但如果我触摸拍摄按钮它会停止旋转。

local isShooting = false

function resetShooting()
    isShooting = false
end

function holdingLeft()
    if isPressed == true then
        if rotationOfship > 0 then
            if touchedXTimes < 10 then
                rotationOfship = rotationOfship-2
                touchedXTimes = touchedXTimes + 1
                ship.rotation = rotationOfship
            elseif touchedXTimes > 9 then 
                rotationOfship = rotationOfship-5
                touchedXTimes = touchedXTimes + 1
                ship.rotation = rotationOfship  
            elseif touchedXTimes > 29 then
                rotationOfship = rotationOfship-8
                touchedXTimes = touchedXTimes + 1
                ship.rotation = rotationOfship                                       
            end
        else 
            rotationOfship = 360
        end
    elseif isPressed == false then 
        timer.cancel(tmr_hold)
    end
end

function holdingRight()
    if isPressed == true then
        if rotationOfship < 360 then
            if touchedXTimes < 10 then
                rotationOfship = rotationOfship+2
                touchedXTimes = touchedXTimes + 1
                ship.rotation = rotationOfship
            elseif touchedXTimes > 9 then 
                rotationOfship = rotationOfship+5
                touchedXTimes = touchedXTimes + 1
                ship.rotation = rotationOfship  
            elseif touchedXTimes > 29 then
                rotationOfship = rotationOfship+8
                touchedXTimes = touchedXTimes + 1
                ship.rotation = rotationOfship                                       
            end 
            print(touchedXTimes)
        else 
            rotationOfship = 0
        end
    elseif isPressed == false then 
        timer.cancel(tmr_hold)
    end
end

function onTouch(event)
    if event.target.name == "left" then
        if event.phase == "began" then
            isPressed = true
            display.getCurrentStage():setFocus( event.target )
            event.target.isFocus = true
            if tmr_hold ~= nil then timer.cancel(tmr_hold) end
            tmr_hold = timer.performWithDelay( 8, holdingLeft, 0)
        elseif event.target.isFocus then
            if event.phase == "ended" or event.phase == "cancelled" then
                isPressed = false
                timer.cancel(tmr_hold)
                touchedXTimes = 0   
                display.getCurrentStage():setFocus( nil )
                event.target.isFocus = false
            end
        end
    end
    if event.target.name == "right" then 
        if event.phase == "began" then
            isPressed = true
            display.getCurrentStage():setFocus( event.target )
            event.target.isFocus = true
            if tmr_hold ~= nil then timer.cancel(tmr_hold) end
            tmr_hold = timer.performWithDelay( 8, holdingRight, 0)
        elseif event.target.isFocus then
            if event.phase == "ended" or event.phase == "cancelled" then
                isPressed = false
                timer.cancel(tmr_hold)
                touchedXTimes = 0   
                display.getCurrentStage():setFocus( nil )
                event.target.isFocus = false
            end
        end
    end
    if event.target.name == "laser" then
        if event.phase == "began" and isShooting == false then
            isShooting = true
            display.getCurrentStage():setFocus( event.target )
            event.target.isFocus = true


            bullet = display.newImageRect("images/laser.png",math.random(5,20),5/2)
                bullet.x = halfW
                bullet.y = halfH                
                bullet.name = "bullet"
                bullet.rotation = rotationOfship-90
                physics.addBody( bullet, "dynamic", { isSensor=true, radius=10} )
                group:insert(bullet)

            ship:toFront()

            audio.play( laserSound, { channel=2, loops=0}  )

            local newX, newY = pointAtDistance(rotationOfship-90, 400)

            bullet:setLinearVelocity( newX/2, newY/2 )

            tmr_shoot = timer.performWithDelay( math.random(300,400), resetShooting, 1) 
        elseif event.target.isFocus then
            if event.phase == "ended" or event.phase == "cancelled" then
                isShooting = false
                timer.cancel(tmr_shoot)
                display.getCurrentStage():setFocus( nil )
                event.target.isFocus = false
            end
        end
    end
    return true
end

dpad_left_circle:addEventListener( "touch", onTouch )
dpad_right_circle:addEventListener( "touch", onTouch )
laser_button_circle:addEventListener( "touch", onTouch )

1 个答案:

答案 0 :(得分:0)

我建议将其分为三种方法而不是一种:

dpad_left_circle:addEventListener( "touch", onTouchLeft )
dpad_right_circle:addEventListener( "touch", onTouchRight )
laser_button_circle:addEventListener( "touch", onTouchLaser )

代码将更清晰,您可以同时触摸不同的按钮。