【问题标题】:Attempt to index global 'object' (a nil value)尝试索引全局“对象”(零值)
【发布时间】:2015-10-27 12:14:25
【问题描述】:

所以这是我得到的错误:

Game.lua:66: attempt to index global 'Spears' (a nil value)
stack traceback:
                         Game.lua:66: in function '_listener'

这是一些代码,显示了错误发生的位置:

    local Spears = {} 
    local SpearsTimer 
    local SpearsCounter = 1


    local delayTimer
    local removeListeners




end
end




local field = display.newCircle( 0, 0, 0 ) ; field.alpha = 0.3
field.name = "field"
field.x = display.contentCenterX ; field.y = display.contentCenterY
physics.addBody( field, "static", { isSensor=true, radius=320 } )






    local spawnSpears = function() 
        local Fall = math.random(display.contentWidth * -0.2, display.contentWidth * 1.2) 
        Spears[SpearsCounter] = display.newImageRect( "Spear.png", 15, 50 ) 
        Spears[SpearsCounter].x = Fall 
        Spears[SpearsCounter].y = -200  
        physics.addBody( Spears[SpearsCounter], "dynamic", {bounce = 0} ) 
        --Spears[SpearsCounter].collision = onCollision 
        --Spears[SpearsCounter]:addEventListener( "collision", Spears[SpearsCounter] ) 
        transition.to( Spears[SpearsCounter], { rotation = Spears[SpearsCounter].rotation+720, time=15000, onComplete=spinImage } ) 
        Spears[SpearsCounter].gravityScale = 0.5 
        sceneGroup:insert(Spears[SpearsCounter])
        group:insert(Spears[SpearsCounter])
        SpearsCounter = SpearsCounter + 1
    end 


   SpearsTimer = timer.performWithDelay( 5000, spawnSpears, -1 )
The Error points to line 66, which is this line of code:

        Spears[SpearsCounter] = display.newImageRect( "Spear.png", 15, 50 ) 

那我做错了什么?

哦,请记住,我正在尝试让对象在整个屏幕中随机生成并掉落/到达屏幕中心。我放了径向重力。

【问题讨论】:

  • 嗨!欢迎来到 Stackoverflow!你检查过图片的路径是否正确吗?
  • 为什么要这样缩进代码

标签: lua sdk null coronasdk


【解决方案1】:

问题是你已经声明了

    local Spears = {}

在该函数之外无法访问的函数。尝试在函数和访问之外声明它。了解变量的范围。

【讨论】:

    【解决方案2】:

    这很可能是范围问题。本教程将引导您了解范围:

    https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

    【讨论】:

    • 此链接已损坏
    猜你喜欢
    • 2013-06-07
    • 2020-03-08
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 2015-02-18
    • 2020-04-27
    相关资源
    最近更新 更多