Accessing my class method from a collision event with event.other?

– I have a class

[code]
function angledGun.new() – constructor

local newAngledGun = {
display = display.newImageRect(“arrowLeft.png”, 50,50),
id = 1,
}
return setmetatable( newAngledGun, angledGun_mt )
end
–This also has a property not seen here… .name = “angledGun”

function angledGun:test()
print(“test”)
end


–And Im trying to do this in another part of the code

local function bulletCollision(self,event)
if event.other.type == “weapon” then
print(event.other.name)
event.other:test()
–event.other.display:test() << this also does not work
end
end

–Im able to get the print statement to print off “angledGun”
–but cant get the method to work???
[/code] [import]uid: 28912 topic_id: 34257 reply_id: 334257[/import]

I’m very very new to Corona but with experience from other engines I would say that you MAY have to somehow cast it to your class. Try googling on how to cast in Corona and see if that helps.

BTW, why test() should be accessed via display?! My best bet would be event.other:test() because, from what I understand from Corona as of now, you are adding test method to the angledGun class (or whatever it is called in Corona/Lua as they are not traditional OOP’s) so in your collision test, you check to get a “weapon” (even though I can’t see a “type” field on your anglegGun) and that test method is defined under angledGun, not display.

Be warned that I may be completely wrong as I’m learning, just wanted to give you my 2cents that might help. [import]uid: 206803 topic_id: 34257 reply_id: 136220[/import]

I figured out how to get it to work

I added this property to my class self.display.self = self

And was then able to access the method like

function collision(self, event)
event.other.self:Method()
end
[import]uid: 28912 topic_id: 34257 reply_id: 136241[/import]

I’m very very new to Corona but with experience from other engines I would say that you MAY have to somehow cast it to your class. Try googling on how to cast in Corona and see if that helps.

BTW, why test() should be accessed via display?! My best bet would be event.other:test() because, from what I understand from Corona as of now, you are adding test method to the angledGun class (or whatever it is called in Corona/Lua as they are not traditional OOP’s) so in your collision test, you check to get a “weapon” (even though I can’t see a “type” field on your anglegGun) and that test method is defined under angledGun, not display.

Be warned that I may be completely wrong as I’m learning, just wanted to give you my 2cents that might help. [import]uid: 206803 topic_id: 34257 reply_id: 136220[/import]

I figured out how to get it to work

I added this property to my class self.display.self = self

And was then able to access the method like

function collision(self, event)
event.other.self:Method()
end
[import]uid: 28912 topic_id: 34257 reply_id: 136241[/import]