hi,
this seems to work but could someone tell me if I am taking the right approach please? I come from an AS3 background so this is a bit like how I would do it, but I’m not sure of the specific Lua implementation for classes. Please note this is a simplified version and would need to be more adaptable for different graphics type’s etc… and I may decide to add the physics body definition to the class as well
My “Class” Object (BallActor.lua)
[lua]BallActor = {} – Create a table to hold the class methods
local sprite
function BallActor:new(x,y,img) – Constructor
local object = { x = x, y = y, img = img }
setmetatable(object, { __index = BallActor }) – Inheritance
sprite = display.newImage(img);
sprite.x = 60 + math.random( 160 )
sprite.y = -100
sprite.id= string.gsub(img, “.png”,"")
object.sprite = sprite
return object
end
function BallActor:describe()
print("I am BallActor with sprite id = "…sprite.id)
end[/lua]
then to use it:
[lua]require(“BallActor”)
local x = math.random(100)
local y= math.random(100)
local img = “ball.png”
obj = BallActor:new(x,y,img)
obj.describe()
physics.addBody(obj.sprite, { density=1.2, friction=0.3, bounce=0.3} )[/lua]
thanks for any advice
j. [import]uid: 6645 topic_id: 3407 reply_id: 303407[/import]