简体   繁体   English

Corona SDK(LUA) - 尝试调用upvalue'spawnEnemy'(零值)

[英]Corona SDK(LUA) - attempt to call upvalue 'spawnEnemy'(a nil value)

I just trying to add eventListener to a object, which should disappear when I tap on it. 我只是尝试将eventListener添加到一个对象,当我点击它时它应该消失。 But I get error mentioned in the title. 但我在标题中提到了错误。 Here is my whole code at this point : 这是我的整个代码:

-- housekeeping stuff

display.setStatusBar(display.HiddenStatusBar)

local centerX = display.contentCenterX
local centerY = display.contentCenterY

-- set up forward references

local spawnEnemy

-- preload audio

-- create play screens

local function createPlayScreen()

    local bg = display.newImage("background.png")
    bg.y = 130
    bg.x = 100
    bg.alpha = 0

    local planet = display.newImage("planet.png")
    planet.x = centerX
    planet.y = display.contentHeight +60
    planet.alpha = 0

    transition.to( bg,  { time = 2000, alpha = 1,  y = centerY, x = centerX } )

    local function showTitle()
        local gametitle = display.newImage("gametitle.png")
        gametitle.alpha = 0
        gametitle:scale (4, 4)
        transition.to( gametitle, { time = 500, alpha = 1, xScale = 1, yScale = 1 })
        spawnEnemy()
    end 
    transition.to( planet,  { time = 2000, alpha = 1,  y = centerY, onComplete = showTitle } )
end

-- game functions

local function shipSmash(event)

    local obj = event.target
    display.remove( obj )

end

local function spawnEnemy()

    local enemy = display.newImage("beetleship.png")
    enemy.x = math.random(20, display.contentWidth - 20)
    enemy.y = math.random(20, display.contentHeight - 20)
    enemy:addEventListener ( "tap", shipSmash )

end


local function startGame()

end


local function planetDamage()

end


local function hitPlanet(obj)

end




createPlayScreen()
startGame()

And here is how error window looks like : 这是错误窗口的样子:

在此输入图像描述

I'm kinda new in this area(LUA programming) so sorry for maybe dumb syntax error or something, but what I saw is that this error shows up after I write this line of code: enemy:addEventListener ( "tap", shipSmash ) 我是这个领域的新手(LUA编程)所以很抱歉可能是愚蠢的语法错误或其他什么,但我看到的是我写这行代码后出现这个错误: enemy:addEventListener ( "tap", shipSmash )

Change local function spawnEnemy() to function spawnEnemy() as this variable was already declared earlier. local function spawnEnemy()更改为function spawnEnemy()因为此变量已在前面声明过。 Yes, this is typical Lua pitfall for beginners. 是的,这是初学者的典型Lua陷阱。

You've declared spawnEnemy as a local variable twice. 您已将spawnEnemy声明为局部变量两次。 That's allowed (the second one hides the first where ever the second one is in scope) but that's not what you wanted. 这是允许的(第二个隐藏第一个,其中第二个在范围内),但这不是你想要的。

You have correctly declared a local variable and captured it in showTitle . 您已正确声明了一个局部变量并在showTitle捕获了它。 To set that very same variable later, use an assignment statement without prefixing it with local . 要在以后设置同一个变量,请使用赋值语句,而不使用local前缀。 You can assign it a function definition using the "anonymous" function syntax: 您可以使用“匿名”函数语法为其分配函数定义:

spawnEnemy = function() 
   ...
end 

Actually, in Lua, all functions are anonymous since they are just values. 实际上,在Lua中, 所有函数都是匿名的,因为它们只是值。 But, for debugging, it is helpful to have a name associated with a function. 但是,对于调试,使用与函数关联的名称会很有帮助。 In stack traces, the name of the variable used to call the function is used, where possible. 在堆栈跟踪中,尽可能使用用于调用该函数的变量的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM