簡體   English   中英

Corona SDK-點擊和觸摸單個對象上的事件,對象旋轉

[英]Corona SDK - Tap and Touch Event on a single object, object rotation

我正在嘗試做一個模擬行星繞太陽旋轉的應用程序。 我希望當應用程序啟動時,行星停止運轉。 當我第一次按下太陽時,行星開始旋轉,而當我第二次按下時,行星停止。 我還希望,如果我向上滑動,行星開始旋轉得更快,而當我向下滑動時,行星減速。

這是我所做的:

-- Sun
local sun = display.newImage ( "Sole.png")
sun.x = display.contentCenterX
sun.y = display.contentCenterY

-- First Planet
local group1 = display.newGroup()
p1 = display.newImage("P1(2).png")
group1:insert(p1)
group1.x = sole.x
group1.y = sole.y
p1.x = 270 
p1.y = 0

-- Second Planet

local group2 = display.newGroup()
p2 = display.newImage("P2.png")
group2:insert(p2)
group2.x = sole.x
group2.y = sole.y
p2.x = -270
p2.y = 0

local function increaseSpeed(event)

   group1.rotation = group1.rotation + 1
   group2.rotation = group2.rotation + 1

end

local function decreaseSpeed(event)

   group1.rotation = group1.rotation - 1
   group2.rotation = group2.rotation - 1

end

-- * State 1: The planets begins to rotate

function state1( event )
   print("state1")
   sun.enterFrame = increaseSpeed
   Runtime:addEventListener("enterFrame", sun)
   sun:removeEventListener( "tap", state1 )             
   timer.performWithDelay( 1, addListener2 )                
   sun:addEventListener( "touch", swipe)
   return true
end

-- * State 2: The planets stops

function state2( event )
   print("state2")
   sole:removeEventListener( "touch", swipe)
   Runtime:addEventListener("enterFrame")                               
   sun:removeEventListener( "tap", state2 )             
   timer.performWithDelay( 1, addListener1 )                
   return true
end

function addListener2()
   sun:addEventListener( "tap", state2 )
end

function addListener1()
   sun:addEventListener( "tap", state1)
end

-- Swipe function

local beginX
local beginY
local endX
local endY

local xDistance
local yDistance

function checkSwipeDirection()

    xDistance =  math.abs(endX - beginX)
    yDistance =  math.abs(endY - beginY)

    if xDistance > yDistance then
            if beginX > endX then
                    print("swipe left")
            else
                    print("swipe right")
            end
    else
            if beginY > endY then
                    print("swipe up")
                    timer.performWithDelay(10, increaseSpeed ,0)
            else
                    print("swipe down")
                    timer.performWithDelay(10, decreaseSpeed ,0)
            end
    end

end

function swipe(event)
   if event.phase == "began" then
            beginX = event.x
            beginY = event.y
   end

   if event.phase == "ended"  then
            endX = event.x
            endY = event.y
            checkSwipeDirection();
   end

   return true
end

我的問題是,如果我處於狀態1,我向上滑動,然后按太陽,我處於狀態2,但是即使我刪除了事件運行時enterFrame,行星仍繼續旋轉。

有人可以幫我嗎? 謝謝 :)

我可以在state2中看到您寫過:

Runtime:addEventListener("enterFrame") 

您應該寫:

Runtime:removeEventListener("enterFrame", sun)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM