How can I cast the parent of the target of an event as a class object that I have previously defined?

I’m using the little class.lua found here to make a solar system with planets. All the bodies in the solar system are of the Body class, and I make an array of bodies to hold all the bodies in the system. Each body has an id, and as long as I’m using the bodies as themselves, I can get the id and do something with it, like treating the Sun as a special case.

Each body is represented visually by Corona display.newCircle(), and I add an event listener for “touch” events to each of those. Then when I’m in the onTouch function, I can get the parent of the circle like this:

    local function onTouch( event )         local t = event.target         local phase = event.phase         if "began" == phase then             local parent = t.parent  

Looking at the table id’s I’ve verified that the parent of the target is indeed the table of the Body object. What I want to do is get parent.id, so I can know which body I’m dealing with, but the “id” attribute only exists for Body objects. So the question is, how can I cast event.target.parent as a Body object so I can access its attributes and methods?

** Nevermind, the parent of a newCircle is display, so it wouldn’t work the way I wanted it to anyway.

I’m not sure I understood what you want, but the only thing you have to do is reference the body object with the graphic object. An easy way to do so, is to create them both as one object.

[LUA]

local planet = {}

local body = display.newCircle(…)

physics.addBody(planet_body, … )

planet.body = body

body.super = planet

local graphic = display.newCircle(…)

local planet.graphic = graphic

graphic.super = planet

function graphic:touch(event)

   local phase = event.phase

   local planet = self.super

  

   if phase =“began” then

      planet:explode() – or whatever you wanne do

   end

end

graphic:addEventListener(“touch”)

[/LUA]

Excellent! Thanks a bunch, I didn’t know I could add a .super attribute, but I guess that goes with the way lua tables work. In my function that initializes the display.newCircle I do this:

    self.Orb = display.newCircle(group, 0, 0, self.orbR)     self.Orb.super = self

Then the onTouch function looks like this:

    local function onTouch( event )         local t = event.target         local phase = event.phase         if "began" == phase then             editingBody = t.super             editingBody:drawInfo(editingText) 

Then there’s a Body class function that looks like this:

function Body:drawInfo(editingText)     editingText:setTextColor(self.red, self.green, self.blue, 192)     editingText.x = display.contentWidth - 100     editingText.y = 10+3\*25     editingText.text = string.format("ID       %2d\nmass %6.2f\nVx   %6.2f\nVy   %6.2f",self.id, self.mass, self.V.x, self.V.y) end

Works like a charm and takes less code than what I was doing.

I’m not sure I understood what you want, but the only thing you have to do is reference the body object with the graphic object. An easy way to do so, is to create them both as one object.

[LUA]

local planet = {}

local body = display.newCircle(…)

physics.addBody(planet_body, … )

planet.body = body

body.super = planet

local graphic = display.newCircle(…)

local planet.graphic = graphic

graphic.super = planet

function graphic:touch(event)

   local phase = event.phase

   local planet = self.super

  

   if phase =“began” then

      planet:explode() – or whatever you wanne do

   end

end

graphic:addEventListener(“touch”)

[/LUA]

Excellent! Thanks a bunch, I didn’t know I could add a .super attribute, but I guess that goes with the way lua tables work. In my function that initializes the display.newCircle I do this:

    self.Orb = display.newCircle(group, 0, 0, self.orbR)     self.Orb.super = self

Then the onTouch function looks like this:

    local function onTouch( event )         local t = event.target         local phase = event.phase         if "began" == phase then             editingBody = t.super             editingBody:drawInfo(editingText) 

Then there’s a Body class function that looks like this:

function Body:drawInfo(editingText)     editingText:setTextColor(self.red, self.green, self.blue, 192)     editingText.x = display.contentWidth - 100     editingText.y = 10+3\*25     editingText.text = string.format("ID       %2d\nmass %6.2f\nVx   %6.2f\nVy   %6.2f",self.id, self.mass, self.V.x, self.V.y) end

Works like a charm and takes less code than what I was doing.