我试图为智能手机制作一个简单的视频游戏,好吧,我现在要做的是,当我点击'volMusicTickOn'对象时,它必须消失,音乐停止。我已经用函数侦听器完成了此操作,但是在调用侦听器时遇到错误,这是代码:
local settings = composer.newScene()
local particlesTable = {}
local particlesGroup = display.newGroup()
local options =
{
width = 600,
height = 600,
numFrames = 2,
sheetContentWidth = 1200,
heetContentHeight = 600
}
local tickSheet = graphics.newImageSheet( "rectTick_sheet.png", options )
--
--
function settings:create( event )
-- Dichiarazione oggeti e codice eseguibile
num=0
local sceneGroup = self.view
background = display.newImageRect( sceneGroup, "background.png", 1280 , 720 )
background.x = display.contentCenterX
background.y = display.contentCenterY
volMusicTickOn = display.newImageRect( "rectTick_on.png", 100, 100 )
volMusicTickOn.name = "volMusicTickOn"
volMusicTickOn.x = display.contentCenterX - 250
volMusicTickOn.y = display.contentCenterY - 100
end
function settings: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)
elseif ( phase == "did" ) then
if event.phase=="began"then
end
if event.phase=="ended"then
end
end
end
function settings:hide( 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)
elseif ( phase == "did" ) then
-- Code here runs when the scene is entirely on screen
end
end
function settings:destroy( event )
local sceneGroup = self.view
end
-- Funzioni
local function remove(particle)
display.remove( particle )
table.remove( particlesTable, num)
end
local function removeParticle( particle )
timer.performWithDelay(1000, transition.to( particle, {alpha=0, time=350, onComplete=remove(particle) } ))
end
local function removetotally( particle )
display.remove( particle )
table.remove( particlesTable, num )
end
function dimParticle( particle )
transition.to( particle, {alpha=0, time=500, onComplete = removetotally, onCompleteParams = particle})
end
local function generateParticles()
local xy=math.random(30,70)
local newParticle = display.newImageRect( particlesGroup, objectSheet , chooseColor(math.random(3)), xy, xy )
table.insert( particlesTable, newParticle )
num = num+1
newParticle.x = (math.random(1280)*math.random(88932))%1280
newParticle.y = (math.random(720)*math.random(13546))%720
newParticle.alpha = 0
transition.to( newParticle, {alpha=1, time=150, onComplete=dimParticle, onCompleteParams=newParticle})
end
local function generateParticlesSmall()
local xy=math.random(5,15)
local newParticle = display.newImageRect( particlesGroup, objectSheet , 1, xy, xy )
table.insert( particlesTable, newParticle )
num = num+1
newParticle.x = (math.random(1280)*math.random(88932))%1280
newParticle.y = (math.random(720)*math.random(13546))%720
newParticle.alpha = 0
transition.to( newParticle, {alpha=1, time=150, onComplete=dimParticle, onCompleteParams=newParticle})
end
local function chooseColor(n)
if n==1 then
return 8
elseif n==2 then
return 9
elseif n==3 then
return 10
end
end
function changeMusicVolumeStatus(event)
if audio.isChannelPaused( 1 ) then
audio.resume( 1 )
else
audio.pause( 1 )
end
return true
end
local function changeEffectsVolumeStatus(event)
if audio.isChannelPaused( 2 ) then
audio.resume( 2 )
else
audio.pause( 2 )
end
return true
end
-- Chiamate e Listener Del Runtime
bigPTimer1=timer.performWithDelay( 1, generateParticles, 0 )
bigPTimer2=timer.performWithDelay( 1, generateParticles, 0 )
smallPTimer=timer.performWithDelay( 1, generateParticlesSmall, 0 )
settings:addEventListener( "create", settings )
settings:addEventListener( "show", settings )
settings:addEventListener( "hide", settings )
settings:addEventListener( "destroy", settings )
volMusicTickOn:addEventListener( "tap", changeMusicVolumeStatus )
volMusicTickOff:addEventListener( "tap", changeMusicVolumeStatus )
--
return settings
这是我得到的错误
16:49:45.126 ERROR: Runtime error
16:49:45.126 C:UserslpasiDownloadsProgetto 1-20170725T134427Z-001Progetto 1settings.lua:167: attempt to index global 'volMusicTickOn' (a nil value)
16:49:45.126 stack traceback:
16:49:45.126 [C]: in function 'error'
16:49:45.126 ?: in function 'gotoScene'
16:49:45.126 C:UserslpasiDownloadsProgetto 1-20170725T134427Z-001Progetto 1menu.lua:36: in function '_onRelease'
16:49:45.126 ?: in function '?'
16:49:45.126 ?: in function <?:654>
16:49:45.126 ?: in function <?:169>
我也尝试使用表侦听器,但没有任何变化,请我知道您是否解决了我的问题,谢谢。
在尝试引用volMusicTickOn
之前,您确定调用settings:create
吗?因为settings:create
定义了volMusicTickOn
,因此在调用之前会nil
它。
在我看来,create
事件可能直到您第一次引用volMusicTickOn
之后才会触发。您的代码(因为它是基于事件的)不能保证运行此行设置的内容:
settings:addEventListener( "create", settings )
在此行之前:
volMusicTickOn:addEventListener( "tap", changeMusicVolumeStatus )
volMusicTickOff:addEventListener( "tap", changeMusicVolumeStatus )
而这,volMusicTickOn
可能是nil
解决方案是将这些行实际移动到settings:create
,以便您知道volMusicTickOn
肯定不是nil
!