0
我想通过在屏幕上滑动手指来改变场景与导演类。但是,它不能正常工作。场景不按顺序。你能检查我的代码,并告诉我,如果我错过了什么?我试图打印currentNumberNo进行调试,发现当我滑动一次光标时它会发生两次更改。可能是什么原因?在Corona的屏幕上滑动手指来改变场景
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
我试过这个,但现在它的复位currentRoomNo在新的场景,因此因为currentRoomNo在main.lua中是1而在第二个房间中(currentRoomNo应该是2但是1),所以它不会到达第三个场景,所以它增加到2,这又是它本身。我们不能修改main.lua中的currentRoomNo吗?这不是全球吗? –
哦,我明白了。改变在main.lua中创建的变量是有问题的。当您尝试在另一个模块中更改它时,corona的行为就像您尝试使用相同名称创建新值一样。所以基本上,你在这个模块中创建一个新的变量。有一个简单的方法来解决这个问题:你应该在主要的lua文件中创建你的变量,并且你应该创建一个函数来改变这个变量的值,这样你就可以实现你想要的。 –
谢谢,Doğancan。 =) –