0

我正在尝试做一个模拟行星围绕太阳旋转的应用程序。我希望当应用程序启动时,行星停止。当我第一次按下太阳时,行星开始旋转,当我第二次按下时,行星停止。我还希望,如果我向上滑动,行星开始旋转得更快,而当我向下滑动时,行星会减速。

这是我所做的:

-- 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。

有人可以帮助我吗?谢谢 :)

4

1 回答 1

1

我可以在 state2 中看到您写道:

Runtime:addEventListener("enterFrame") 

你应该写:

Runtime:removeEventListener("enterFrame", sun)
于 2013-11-15T04:49:41.737 回答