"class"-based display objects

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]

hmm i think that would cause a problem in my collision

[lua]event.object1:removeSelf()[/lua]

since this refers to obj.sprite, i’m not removing obj (my BallActor class instance) am i? [import]uid: 6645 topic_id: 3407 reply_id: 10220[/import]

Hi jmp909,

would event.object1.sprite:removeSelf() work for you?

I have not yet played much with sprites, so not sure if this works, but this is what I would do logically to remove the sprite,not the object.

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3407 reply_id: 10330[/import]

event.object1 *is* the sprite, but i can set a dynamic property on the sprite called “actor” that points to my class

i need to clean this up a bit, i’ll post an update when it’s working properly :S [import]uid: 6645 topic_id: 3407 reply_id: 10348[/import]

@jmp909
I was wondering if I could get your skype details please.

I’m alcamie on skype.

appreciated

Chris [import]uid: 9457 topic_id: 3407 reply_id: 11070[/import]

I don’t really know how metatables interact with the display object “tables” that Corona has, but I’m wondering if you even need a separate “sprite” entry in “object” or if you can simply make “object” a display object. That’s similar to how I do things except I don’t use metatables, taking advantage of the fact that all objects are really tables:
http://developer.anscamobile.com/content/display-objects#Display_Objects_vs._Tables:_A_Technical_Discussion

EDIT: Actually the second line of that link specifically states “you cannot set the metatables of display objects.” doh

EDIT2: Just noticed your newer thread, posting there too. [import]uid: 12108 topic_id: 3407 reply_id: 21541[/import]