event.contact is sometimes nil

I copied the code from this tutorial and added a rectangle physic body at the bottom of the screen.

The program occasionally fails because event.contact is sometimes nil

Here is the code:

-- Set up physics engine local physics = require( "physics" ) physics.start() physics.setDrawMode( "normal" ) physics.setGravity( 0,0 ) --platform local rect = display.newRect( display.contentCenterX, display.contentHeight-25, display.contentWidth, 50 ) physics.addBody( rect, "static") local armoredSkeleton = display.newImageRect( "skeleton.png", 200, 256 ) armoredSkeleton.x, armoredSkeleton.y = display.contentCenterX, display.contentCenterY local armorPieces = { helmet = { -38,-103,-26,-118,-15,-120,-4,-118,8,-103,8,-63,-38,-63 }, mantle = { -68,-55,-52,-63,20,-63,37,-55,47,-44,55,-20,-89,-20,-80,-44 }, chest = { 44,-44,54,54,-85,54,-76,-44 }, shield = { 88,-10,98,13,86,65,98,42,66,80,41,86,41,-33,66,-28 }, faulds = { 48,54,53,80,-87,80,-85,54 }, legLeft = { -34,80,-34,127,-72,127,-72,80 }, legRight = { 43,80,43,127,5,127,5,80 } } physics.addBody( armoredSkeleton, "dynamic", { shape = armorPieces["helmet"] }, { shape = armorPieces["mantle"] }, { shape = armorPieces["chest"] }, { shape = armorPieces["shield"] }, { shape = armorPieces["faulds"] }, { shape = armorPieces["legLeft"] }, { shape = armorPieces["legRight"] } ) local armorStates = { true, true, true, true, true, true, true } local function skeletonHit( self, event ) -- Dictate the collision behavior based on the armor element state if ( armorStates[event.selfElement] == false ) then -- Use physics contact to void collision event.contact.isEnabled = false else -- Set the associated armor element state to "destroyed" armorStates[event.selfElement] = false end end armoredSkeleton.preCollision = skeletonHit armoredSkeleton:addEventListener( "preCollision" )

I was getting that error too, a good idea is to check for contact:

if (armorStates[event.selfElement] == false and contact) then

I was getting that error too, a good idea is to check for contact:

if (armorStates[event.selfElement] == false and contact) then