通过在Corona屏幕上滑动手指来改变场景

时间:2013-07-30 12:07:52

标签: corona slide touch-event

我试图通过在屏幕上滑动手指来改变导演类的场景。但是,它无法正常工作。场景没有按顺序排列。你可以检查我的代码并告诉我,如果我错过了什么?我试图打印currentNumberNo进行调试,发现当我将光标滑动一次时它会改变两次。可能是什么原因?

main.lua

local director = require "director"
--GLOBALS--
W = display.contentWidth
H = display.contentHeight
room={"kitchen", "bedroom", "bathroom"}
currentRoomNo = 1

director:changeScene("kitchen")

kitchen.lua:

module(..., package.seeall)
local director = require "director"

function new()

group = display.newGroup()

local bg = display.newRect(0,0,W,H); bg:setFillColor(255,250,30)
group:insert(bg)

---[[
local function slide(event) 
if event.phase == "ended" or event.phase == "cancelled" then
    local dif = event.xStart-event.x
    if dif> 30 then
        if currentRoomNo==#room then
            currentRoomNo = 1
        else
            currentRoomNo = currentRoomNo + 1
        end
    director:changeScene(room[currentRoomNo])
    elseif dif< -30 then
        if currentRoomNo==1 then
            currentRoomNo = #room
        else
            currentRoomNo = currentRoomNo - 1
        end
    director:changeScene(room[currentRoomNo])
    end 
end
return true
end

Runtime:addEventListener("touch", slide)
--]]
return group

end

1 个答案:

答案 0 :(得分:2)

试试这个:

module(..., package.seeall)
local director = require "director"

function new()
local isChanging = false
group = display.newGroup()

local bg = display.newRect(0,0,W,H); bg:setFillColor(255,250,30)
group:insert(bg)

---[[
local function slide(event) 
if ( event.phase == "ended" or event.phase == "cancelled" ) and isChanging == false then
    local dif = event.xStart-event.x
    if dif> 30 then
        if currentRoomNo==#room then
            currentRoomNo = 1
        else
            currentRoomNo = currentRoomNo + 1
        end
    director:changeScene(room[currentRoomNo])
    isChanging = true
    elseif dif< -30 then
        if currentRoomNo==1 then
            currentRoomNo = #room
        else
            currentRoomNo = currentRoomNo - 1
        end
    director:changeScene(room[currentRoomNo])
    isChanging = true
    end 
end
return true
end

Runtime:addEventListener("touch", slide)
--]]
return group

end