2017-10-10 108 views
-1

我试图做一个简单的视频游戏的智能手机,那么,现在我要做的是,当我的“volMusicTickOn”对象上点击,这必须消失,音乐停止。我做这个用的功能听众,但我在打电话的听众得到了一个错误,下面的代码:尝试索引全局'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:\Users\lpasi\Downloads\Progetto 1-20170725T134427Z-001\Progetto 1\settings.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:\Users\lpasi\Downloads\Progetto 1-20170725T134427Z-001\Progetto 1\menu.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> 

错误我试图也用表听众,但没有任何改变,请求知道如果你解决我的问题,谢谢你。

+0

看来volMusicTickOn是零,请检查您的路径为“rectTick_on.png”,是否正确,文件名是否正确? –

+0

@Abadziluk yup,这不是问题,试图评论听众,它通常出现在屏幕上。 –

回答

0

确定settings:create在尝试引用volMusicTickOn之前被调用吗?因为settings:create定义volMusicTickOn,因此这将是nil直到它被调用。

正如我所看到的,create事件可能会在您首次引用volMusicTickOn之后才会被触发。您的代码,因为它是基于事件的,不能保证运行这行设置:

settings:addEventListener("create", settings) 

这一行之前:

volMusicTickOn:addEventListener("tap", changeMusicVolumeStatus) 
volMusicTickOff:addEventListener("tap", changeMusicVolumeStatus) 

而这一点,volMusicTickOn可能是nil

的解决方法是将这些行实际移动到settings:create,因此您知道volMusicTickOn肯定不是nil

+0

非常感谢你。 –

相关问题