I have some IGmines (in-ground mines) that only explode when either the player or an enemy rolls over them.
I am trying to get my bullets to ignore when they collide with the IGmine’s physics body, at the moment the only thing that happens is the bullet gets removed, but I can’t seem to get the bullet to completely ignore the collision.
Should I attached the ‘preCollison’ to the IGmine itself rather than the bullet?
The code below throws an error:
“attempt to index local ‘event’ (a nil value)”
– The “event” it is referring to, is this line: event.other.collType –
(To save you time from having to read through all my code, I took out quite a bit of code and left just enough so you can get a good idea for how I have things setup)
[lua]
local function shootBullet()
local pt = mathapi.rotateAboutPoint( {x=user_turret.x,y=user_turret.y-55}, user_turret, user_turret.rotation - 90 )
local bullet1 = loader:createSpriteFromSHDocument(“Weapon5ex”, “GameFX”, “LevelAssets.pshs”);
– various bullet attributes –
–==================== ‘RemoveSelf’ functions for various objects–====================
function checkNumberOfEnemyTanks()
if EnemyTanks == 0 then
print(" All Enemy Tanks Destroyed ")
--onLevelCompletion()
end
end
function removePlayer()
– code for removing player –
end
–==================== Collision Handling–====================
function preCollision( self, event )
if event.other.collType == “passthru” then
print(" hit passthru object")
event.contact.isEnabled = false
self.isSensor = true ; self.setAsSensor = true
end
end
local onbullet1Collision = function( self, event )
if event.phase == “began” then
preCollision()
PBcollisions = PBcollisions + 1
if event.other.class == “EnemyTank”
then
print(" Hit EnemyTank “)
self:removeSelf()
self = nil
Emitter = Particles.GetEmitter(“E1”)
Emitter.x = event.other.x
Emitter.y = event.other.y
Particles.StartEmitter(“E1”)
display.remove( event.other )
– event.other = nil
EnemyTanks = EnemyTanks - 1
print(” EnemyTank DEAD. Number of EnemyTanks reduced by 1 “)
checkNumberOfEnemyTanks()
end
if event.other.class == “bullet”
then
self:removeSelf()
self = nil
print(” bullets collided&removed “)
end
elseif event.phase == “ended” then
– if PBcollisions == 1 then
self:removeSelf()
self = nil
PBcollisions = 0
print(” Projectile Removed. PBcollisions set to 0 ")
– end
end
end
bullet1.preCollision = preCollision
bullet1:addEventListener( “preCollision” )
bullet1.collision = onbullet1Collision
bullet1:addEventListener( “collision”, bullet1 )
end
[/lua]