简体   繁体   中英

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

I'm trying to do an application that simulates the planets rotation around the sun. I want that when the application starts, the planets are stopped. When I press the sun the first time the planets starts to rotate and when I press the second time, the planets stops. I also want that if I do an up swipe, the planets begins to rotate faster and when I do a down swipe, the planets slows down.

Here's what i've done:

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

My problem is that if I am in the state1, I do the swipe up and the I press the sun, I am in the state2 but the planets continues to rotate even if I removed the Event Runtime, enterFrame.

Can somebody help me ? Thanks :)

I can see in state2 that you wrote :

Runtime:addEventListener("enterFrame") 

You should write :

Runtime:removeEventListener("enterFrame", sun)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM