我正在尝试创建一个类来在我的屏幕上生成一些新对象。我遵循这个教程
我的目标是做一些事情链接这个:
local box = box.new( "First", 0, 62 ) -- Spawn a Box on the Screen on X = 0, Y = 62
这是我的班级:
box.lua
local box = {}
local box_mt = {__index = box } -- metatable
function box.new (nome, x, y)
local newbox = {
local nome = display.newImageRect( "images/box.png", 210, 70 )
newbox.x = x
newbox.y = y
}
return setmetatable( newbox, box_mt )
}
end
到目前为止,只是在level1上做了一个require ("box")
。我的游戏崩溃了
This is the error
Failed to parse error message: error loading module 'level1' from file '/Users/mc309bza/Desktop/Corona/Platform/level1.lua':
/Users/mc309bza/Desktop/Corona/Platform/level1.lua:28: syntax error near 'function'
任何想法?谢谢!
表格右括号位置错误。用途:
local newbox = {}
local nome = display.newImageRect( "images/box.png", 210, 70 )
newbox.x = x
newbox.y = y
不要忘记删除newbox之后的关闭}
。但是,如果您的目的是使框的创建自动化,那么您应该使用函数代替,因为显示。newimag竖立已经是一个类了。使用函数:
function myNewBox(x, y)
local nome = display.newImageRect( "images/box.png", 210, 70 )
nome.x = x
nome.y = y
... other nome settings ...
return nome
end