Lua: how do I add display objects required from external functions to display groups?

匆匆过客 提交于 2019-12-11 20:48:14

问题


In a storyboard scene, I require a bunch of display objects from external functions. When I attempt to add these to the scene's display group, I get the error "table expected."

function scene:createScene(event)
    local group=self.view
    local shieldDisplay = shieldDisplay.new()
    group:insert(shieldDisplay)
end

The external function looks like this:

function shieldDisplay.new()
    shieldDisp = display.newText("Shield: "..tostring(Cshield), 1165, 20, native.systemFont, 30)
    shieldDisp:setTextColor(9,205,235)
end
return shieldDisplay

What am I doing wrong?


回答1:


The return object must be inside on the function that you're calling.

function shieldDisplay.new()
    local shieldDisp = display.newText("Shield: "..tostring(Cshield), 1165, 20, native.systemFont, 30)
    shieldDisp:setTextColor(9,205,235)
    return shieldDisp 
end



回答2:


function scene:createScene(event)
    local group=self.view
    local shieldDisplay = shieldDisplay.new()
    group:insert(shieldDisplay)
end

Try changing it to

function scene:createScene(event)
    local group=self.view
    local shieldDisplay = shieldDisplay.new
    group:insert(shieldDisplay)
end


来源:https://stackoverflow.com/questions/17954046/lua-how-do-i-add-display-objects-required-from-external-functions-to-display-gr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!