How come I get a attempt to call method 'insert' (a nil value)
error on the line containing insert
?
Changing it to instance.sprites = bg
does make it work, but I want to return all sprites in a separate table (sprites).
local writingTool = {}
local _W, _H = display.contentWidth, display.contentHeight
function writingTool:new()
local instance = {}
instance.index = writingTool
setmetatable(instance, self)
instance.sprites = {}
local bg = display.newImage("images/backgrounds/wooden_bg.png")
bg.x = _W/2
bg.y = _H/2
instance.sprites:insert(bg)
return instance
end
return writingTool
Edit: Trying instance.sprites.bg = bg
does not work either. Give this error:
bad argument #-2 to 'insert' (Proxy expected, got nil)
instance.index = writingTool
Should be
instance.__index = writingTool
Though I would remove the above line and implement it in the one below like so:
setmetatable(instance,{__index=writingTool})
Also, t:insert()
or t.insert()
aren't defined by default , to insert elements into a table you use the table.insert
function as defined below:
table.insert (table, [pos,] value)
so you should have table.insert(instance.sprites,bg)
. So with these modifications your function should look like:
function writingTool:new()
local instance = { sprites = {} }
setmetatable(instance, {__index = wirtingTool})
local bg = display.newImage("images/backgrounds/wooden_bg.png")
bg.x = _W/2
bg.y = _H/2
table.insert(instance.sprites,bg)
return instance
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.