【问题标题】:Multitouch detection with holding event Corona SDK多点触控检测与保持事件 Corona SDK
【发布时间】:2015-01-26 22:13:31
【问题描述】:

我有一个大问题,因此我很生气。我不知道如何制作一个处理触摸多个对象的函数。介绍我有什么和应该做什么。 我有 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 )

【问题讨论】:

    标签: android lua coronasdk


    【解决方案1】:

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

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

    代码将更加清晰,您将能够同时触摸不同的按钮。

    【讨论】:

      猜你喜欢
      • 2017-07-05
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 2012-02-26
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多