I need help with this collision function within my game. For some reason, the terminal says that the “removeSelf” method is nil, but I checked the physics engine and it removes the display object even though it displays these errors. How can I stop these errors from occuring?
local function onCollision( self, event )
print( event.other.myName )
if ( event.other.myName == "first bound" ) then
fadeout = transition.to( self, { alpha="0", xScale="0.01", yScale="0.01", time=100 } )
local function closure()
transition.cancel(fadeout)
self.isBodyActive = false
self:removeSelf()
end
timer.performWithDelay(150, closure, 1)
print("removed")
elseif ( event.other.myName == "second bound" ) then
local function closure()
transition.cancel(fadeout)
self.isBodyActive = false
self:removeSelf()
end
timer.performWithDelay(150, closure, 1)
print("removed")
elseif ( event.other.myName == "third bound" ) then
print( self.myName )
print( "allow thru" )
elseif ( event.other.myName == "fourth bound" ) then
print( self.myName )
print( "allow thru" )
elseif ( event.other.myName == nil ) then
print("nil")
else
print("not reconizable collision")
print( event.other )
end
end
Thank you for your help in advance [import]uid: 29220 topic_id: 8539 reply_id: 308539[/import]
you’re not checking for an event.phase. it’ll be trying to call the removeSelf in the “ended” phase as well as the “began” phase. and since you already removed it during “began”, it’ll fail when it calls it again in “ended” [import]uid: 6645 topic_id: 8539 reply_id: 30661[/import]
Ohhhhhh. I didn’t realize I needed to specify. Thank you so much for your input. I am totally new to the corona environment and I am still trying to learn all the necessary commands and dependencies I must include. What I still don’t understand is that when I remove the closure function and timer and just declare the removeSelf function outright in the if block, the code runs without error. Of course, this bypasses the transition and causes the display object to be removed in a choppy manner. Any thoughts on why it works when you condense it? [import]uid: 29220 topic_id: 8539 reply_id: 30754[/import]
no idea, it’s probably an internal flag that prevents the “ended” phase from firing, since you’ve flagged the body for removal during the collision.
although actually have you checked that your closure actually works? if you [lua]print(self)[/lua] do you get a table address? (of the object in collision). or is it nil as well? [import]uid: 6645 topic_id: 8539 reply_id: 30772[/import]
I inserted the print function in the closure and rerun the program and it showed the table reference. I also tried(separately) limiting the collision function with the event.phase in an if block. That eliminated the errors and now it functions properly. Thank you so much for showing me that. It would have taken me a while before I caught on to that detail.
Is there anything else I should watch out for when using collision functions and event.phase’s?
[import]uid: 29220 topic_id: 8539 reply_id: 30886[/import]