help with creating a class OOP

Hi guys,

I am trying to implement a class called key… it works fine… but i need to know how to get the x position and y position of the object.

Also how can I add event listeners

code for key.lua

[code]

local key = {}
local key_mt = { __index = key}

function key.new(startX, startY ) – constructor

local keyWidth = 25
local keyHeight = 30

local charImage = “items/img/key.png”
local key = display.newImageRect(charImage, keyWidth, keyHeight)

key.x = startX
key.y = startY

physics.addBody(key, “static”)

return setmetatable( key, key_mt )
end
function key:getX()

print(key.x)

end

– would i add collision event handlers here?

return key
[/code] [import]uid: 67619 topic_id: 20696 reply_id: 320696[/import]

Try this:

[code]
local key = {}
local key_mt = { __index = key}

function key:new(startX, startY ) – constructor

local keyWidth = 25
local keyHeight = 30

local charImage = “items/img/key.png”
local k = display.newImageRect(charImage, keyWidth, keyHeight)

k.x = startX
k.y = startY

local obj = { displayObject = k }

– i suggest add the physics properties on the code that will create instances of this class
–physics.addBody(key, “static”)

return setmetatable( obj , key_mt )
end

function key:getX()

print(self.displayObject.x)
return self.displayObject.x
end

– would i add collision event handlers here?
– try this. to add collision:
– nameOfObject:addEventListener(‘collision’, nameOfObject)
function key:collision(event)
– do stuff
end

return key
[/code] [import]uid: 71085 topic_id: 20696 reply_id: 82625[/import]