I have just started working on a tower defense style game. I am using OOP in an attempt to keep things manageable, but I am struggling with setting up a collision listener for the towers.
If I draw physics in debug mode, it shows the objects in question overlap, yet there is no collision event thrown.
Tower = {} Tower\_mt = { \_\_index = Tower } function Tower:new( parentGroup, params ) --print("Im in NEW VIRUS") local self = {} setmetatable( self, Tower\_mt ) --A BUNCH OF IRRELEVANT CODE HERE function onLocalCollision( mySelf, event ) if ( event.phase == "began" ) then print( "began: " .. mySelf.type .. " & " .. event.other.type .. " at " .. event.x .. "," .. event.y ) elseif ( event.phase == "ended" ) then print( "ended: " .. event.object1.type .. " & " .. event.object2.type .. " at " .. event.x .. "," .. event.y ) end end -- Add Firing Range Collider print("Printing Params: "..params.spawnX ..",".. params.spawnY ..",".. self.\_myRange) self.\_rangeCollider.isVisible = true physics.addBody( self.\_rangeCollider, "kinematic", {radius=70} ) self.\_rangeCollider.isSensor = true self.\_rangeCollider.collision = onLocalCollision self.\_rangeCollider:addEventListener( "collision", self.\_rangeCollider ) return self end
What I am hoping will happen is that when a new tower is spawned it will instantly start detecting collisions with potential targets.
What “type” of physics objects are the targets? Remember that at least one body in any collision must be “dynamic” type… “kinematic” will not collide with another “kinematic”, for example.
If you’re building a tower defense game, I assume there is no gravity, because the view is looking from above? If so, you can make the range sensors “dynamic”… there is no specific reason to make them kinematic if no physical forces will be applied to them (i.e. gravity).
What “type” of physics objects are the targets? Remember that at least one body in any collision must be “dynamic” type… “kinematic” will not collide with another “kinematic”, for example.
If you’re building a tower defense game, I assume there is no gravity, because the view is looking from above? If so, you can make the range sensors “dynamic”… there is no specific reason to make them kinematic if no physical forces will be applied to them (i.e. gravity).