Hello all,
I’ve been trying to understand how modular classes work. I’ve modified the class so that the return object is a display group. The reason i need this is because my characters have different attachments (weapon, armor)
However when i do the below code, i can’t set the position of the object after creating.
-- main
local npc = require "npcclass"
local npcGroup
local function wipeNPC()
for i = 1, npcGroup.numChildren do
npcGroup[i]:die()
end
display.remove( npcGroup )
end
local function createNPC()
npcGroup = display.newGroup()
local npc1 = npc.new{
name = "nerdypie",
age = 30
}
npc1:greet()
-- set position is not working, so does setting npc1.x, npc1.y externally
npc1:setPos( 100, 100 )
npcGroup:insert( npc1 )
end
local function main()
createNPC()
timer.performWithDelay( 5000, wipeNPC, 1 )
end
main()
-- npc class
local NPC = {}
local NPC\_MT = { \_\_index = NPC }
function NPC.new( params )
local params = params or {}
local newNPC = display.newGroup()
newNPC.name = params.name or "noname"
newNPC.age = params.age or 0
local model = display.newImage( params.model or "missingtexture.png" )
newNPC:insert( model )
model = nil
--local weapon = display.newImage( params.weapon or "missingtexture.png" )
--newNPC:insert( weapon )
--weapon = nil
newNPC.isDead = false
return setmetatable( newNPC, NPC\_MT )
end
function NPC:greet()
print( self.name .. " says hello!" )
end
function NPC:setPos( x, y )
self.x = x
self.y = y
end
function NPC:die()
print( self.name .. " has died!" )
self.isDead = true
end
return NPC
[import]uid: 74723 topic_id: 30327 reply_id: 330327[/import]