Self vs Self

I’m trying to figure out how to keep the game from crashing if a collision occurs between multiple targets. This is from the the Ghosts vs Monsters code:

local onMonsterPostCollision = function( self, event )  
 if event.force \> 1.5 and self.isHit == false then  
 audio.play( monsterPoofSound )  
  
 self.isHit = true  
 print( "Monster destroyed! Force: " .. event.force )  
 self.isVisible = false  
 self.isBodyActive = false  
  
 -- Poof code below --  
 if poofTween then transition.cancel( poofTween ); end  
  
 greenPoof.x = self.x; greenPoof.y = self.y  
 greenPoof.alpha = 0  
 greenPoof.isVisible = true  
  
 local fadePoof = function()  
 transition.to( greenPoof, { time=500, alpha=0 } )   
 end  
 poofTween = transition.to( greenPoof, { time=50, alpha=1.0, onComplete=fadePoof } )  
  
 monsterCount = monsterCount - 1  
 if monsterCount \< 0 then monsterCount = 0; end  
  
 self.parent:remove( self )  
 self = nil  
  
 local newScore = gameScore + mCeil(5000 \* event.force)  
 setScore( newScore )  
 end  
 end  

How would you make ‘self’ recognize two separate objects so that they would both be destroyed? [import]uid: 14032 topic_id: 8994 reply_id: 308994[/import]

Post-collision events include information about both objects:
http://developer.anscamobile.com/content/postcollision [import]uid: 12108 topic_id: 8994 reply_id: 33143[/import]

I think my problem is that I have two monsters and when they collide it’s calling the post-collision event at the same time. Is there a way to double up the postCollision so that it would recognize event.force and there being multiple objects activating it and both being event.objects? [import]uid: 14032 topic_id: 8994 reply_id: 33161[/import]

Still could use some help here. The whole event seems to run through fine. It doesn’t crash until after the onMonsterPostCollision is complete. [import]uid: 14032 topic_id: 8994 reply_id: 33850[/import]

Any more info here would be awesome :slight_smile: [import]uid: 14032 topic_id: 8994 reply_id: 35409[/import]

Try wrapping everything under “if event.phase…” in a 1ms delayed timer and see if that prevents the crashes.

You could be running into the Box2D issue I discovered here:
http://jonbeebe.net/post/2515495262/corona-collision-no-no

The article above describes the workaround in detail.

Hope that helps! [import]uid: 52430 topic_id: 8994 reply_id: 35447[/import]

That’s it! Thanks so much! It is giving me a weird little problem now though. For some reason, the doCollision function is getting called twice for each time a single monster is destroyed.

local onMonsterPostCollision = function( self, event )  
 if event.force \> 1 and self.isHit == false or self.isHit == false and self.x \>= 970 or self.isHit == false and self.x \< 0 then  
 local doCollision = function()  
 audio.play( monsterPoofSound, { channel=5, onComplete=audio.stop( 5 ) } )  
  
 self.isHit = true  
 print("self is hit = true")  
 print( "Zombean squished! Force: " .. event.force )  
 self.isVisible = false  
 print("self is visible = false")  
 self.isBodyActive = false  
 print("self is active = false")  
  
 -- Poof code below --  
 if poofTween then transition.cancel( poofTween ); end  
  
 greenPoof.x = self.x; greenPoof.y = self.y  
 greenPoof.alpha = 0  
 greenPoof.isVisible = true  
  
 local fadePoof = function()  
 transition.to( greenPoof, { time=500, alpha=0 } )   
 end  
 poofTween = transition.to( greenPoof, { time=50, alpha=1.0, onComplete=fadePoof } )  
  
 monsterCount = monsterCount - 1  
 if monsterCount \< 0 then monsterCount = 0; end  
 print("monster count - 1")  
  
 self:removeEventListener( "postCollision", self )  
 self = nil  
  
 local newScore = gameScore + mCeil(5000 \* event.force)  
 setScore( newScore )  
 print( "Score: "..newScore )  
 end  
 local collisionTimer = timer.performWithDelay( 1, doCollision, 1)  
 end  
 end  

Any ideas? [import]uid: 14032 topic_id: 8994 reply_id: 35509[/import]

After Line 30, there was a ‘self.parent:remove( self )’ but I had removed that because it was giving me errors

  
Runtime error  
 ...fRsa+Y19VU+++TI/-Tmp-/TemporaryItems/233/levelac.lua:1843: attempt to index field 'parent' (a nil value)  
stack traceback:  
 [C]: ?  
 ...fRsa+Y19VU+++TI/-Tmp-/TemporaryItems/233/levelac.lua:1843: in function '\_listener'  
 ?: in function <?:441>  
 ?: in function <?:214>  

Would I need to keep that to get rid of the duplication but fix it so it works within the doCollision function? [import]uid: 14032 topic_id: 8994 reply_id: 35511[/import]

Where the collision is happening twice is probably where you would have gotten the crash previously.

What you should do is create a property (such as isHit), set it to false, and then in your collision code, test to see if it’s still false, and then set it to true so the collison “actions” don’t get performed twice.

Let me know if you need more clarification on that… [import]uid: 52430 topic_id: 8994 reply_id: 35558[/import]

I got it! Not fully sure how this works but I had to move the local doCollision = function() to just above the self:removeEventListener( "postCollision", self )

Now it’s time to figure out memory leaks and debugging and ZomBeans is done! … any help there would be awesome, too. And Jon, since this game wouldn’t exist without the Ghosts vs Monsters code, I’d really like to send you a couple copies of my books that sort of lead into the game. So if you want 'em, send me an address I can ship them to - vandono at ratatatgraphics dot com

Thanks so much! [import]uid: 14032 topic_id: 8994 reply_id: 35577[/import]