【问题标题】:Corona SDK - Tap and Touch Event on a single object, object rotationCorona SDK - 在单个对象上点击和触摸事件,对象旋转
【发布时间】:2013-11-15 00:09:04
【问题描述】:

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

这是我所做的:

-- 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,但即使我删除了事件运行时,行星也会继续旋转,进入框架。

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

【问题讨论】:

    标签: events lua rotation touch coronasdk


    【解决方案1】:

    我可以在 state2 中看到你写的:

    Runtime:addEventListener("enterFrame") 
    

    你应该写:

    Runtime:removeEventListener("enterFrame", sun)
    

    【讨论】:

    • 谢谢您的回答!我尝试编写您编写的代码,现在如果我只向上滑动一次,一切正常(行星停止),但第二次如果我向上滑动两次,行星并没有停止。无论如何,谢谢,现在效果更好了!
    • sole:removeEventListener("touch", swipe) state2 中的“sole”是什么?也许您不应该删除“sole”的滑动,或者您必须在行星移动时再次添加事件侦听器。
    猜你喜欢
    • 1970-01-01
    • 2020-11-16
    • 2013-12-26
    • 1970-01-01
    • 2014-01-07
    • 2014-06-20
    • 1970-01-01
    • 2013-04-01
    • 2012-02-26
    相关资源
    最近更新 更多