【问题标题】:attempt to index global 'volMusicTickOn' (a nil value). Listener Errors尝试索引全局“volMusicTickOn”(一个零值)。侦听器错误
【发布时间】:2018-03-22 01:19:06
【问题描述】:

我试图为智能手机制作一个简单的视频游戏,嗯,我现在想做的是,当我点击“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>

我也尝试使用表侦听器,但没有任何改变,如果你解决了我的问题,请告诉我,谢谢。

【问题讨论】:

  • volMusicTickOn 好像是 nil,检查你到“rectTick_on.png”的路径,是否正确,文件名是否也正确?
  • @Abadziluk 是的,这不是问题,尝试评论监听器,它正常显示在屏幕上。

标签: lua null initialization listener coronasdk


【解决方案1】:

您确定在尝试引用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 2015-04-09
    • 2014-01-26
    • 2013-06-07
    相关资源
    最近更新 更多