我正在使用LUA Glider和Level Director X编写游戏,使用TexturePacker创建关卡以创建spritesheets。我在触摸事件处理程序中遇到了一个非常不寻常的事情。
正如您在代码中看到的,我有一个名为“sign”的对象。我为它分配了一个调用函数“flipSign”的触摸事件处理程序。函数翻转符号检查触摸事件是否已经结束,如果是,则将名为“signDirection”的变量增加1并打印该值。
每次运行应用程序并单击Corona Simulator中的符号时,输出都会打印:
1 2 3 4 5
再次单击该符号会将变量再增加5次为10.我的理解是,每次单击符号时,代码只应将变量增加1。
任何帮助理解为什么会发生这种情况将不胜感激。
谢谢!
local composer = require( "composer" )
local scene = composer.newScene()
require("lib.LD_LoaderX")
physics = require ("physics")
physics.start()
local render = require ("render")
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------
local myLevel = {}
local signDirection = 0
local function flipSign( event )
if (event.phase == "ended") then
signDirection = signDirection + 1
print(event.phase)
end
return true
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
-- Code here runs when the scene is first created but has not yet appeared on screen
local sceneGroup = self.view
myLevel= LD_Loader:new(self.view)
myLevel:loadLevel("level_1")
end
-- show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
local sign = myLevel:getLayerObject("island_4", "Sign_2_4").view
sign:addEventListener ( "touch", flipSign )
elseif ( phase == "did" ) then
-- Code here runs when the scene is entirely on screen
end
end
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is on screen (but is about to go off screen)
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
myLevel:removeLevel()
myLevel = nil
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene
答案 0 :(得分:0)
在您提供的代码段中,没有任何内容正在打印signDirection的值。相反,您在NA, NA.1, NA.x ...NA.6
:
flipSign()
应输出:
print(event.phase)
每次在DisplayObject ended
上触摸结束。所以你说你得到的输出
sign
代码段中的任何内容都无法生成。您的代码中是否有其他触摸侦听器?