ok so ive been creating my classes set out by an example on the internet but recently been browsing through LUA wiki and came across a class example using metatables. Ive tried their method and cant get it to work, so if someone could please tell me what im doing wrong and also whats the difference performance wise between my old method and the newer. Ive included my method and the wiki way.
Many thanks.
--MAIN
local Account = require("Account") -- LUA example that does not work for me
local Balloon = require("Balloon") -- a class I created using different method that works
balloon = Balloon.new() -- plenty of joy
acc = Account.create(1000) -- nope no joy ???
acc:withdraw(100)
--ACCOUNT CLASS from wiki
Account = {}
Account.\_\_index = Account
function Account.create(balance)
local acnt = {} -- our new object
setmetatable(acnt,Account) -- make Account handle lookup
acnt.balance = balance -- initialize our object
return acnt
end
function Account:withdraw(amount)
self.balance = self.balance - amount
end
[code]
–BALLOON CLASS
local Balloon = {}
function Balloon.new()
local balloon = display.newImageRect( “balloon.png”, 100, 90 )
balloon.id = “balloon”
balloon.x = 200
balloon.y = 200
physics.addBody( balloon, “dynamic”, { density=0.2, friction=1, bounce=0.0, radius=40 } )
balloon.rotation = 0
balloon.isFixedRotation = true
balloon.health = 100
function balloon:addForce(wind, hotAir)
balloon:applyForce( wind, hotAir, 20, balloon.y )
end
function balloon:applyImpulse(wind, hotAir)
balloon:applyLinearImpulse( wind, hotAir, 20, balloon.y )
end
function balloon:getVelocity()
vx, vy = balloon:getLinearVelocity()
return vy
end
function balloon:destroy()
balloon:removeSelf()
balloon = nil
end
return balloon
end
return Balloon
[/code] [import]uid: 118379 topic_id: 21582 reply_id: 321582[/import]