First, here is a short video of what I’ve got going on:
video (video quality is HD when downloaded)
It’s weird how it only throws an error once I’m “in range” or “in sight” of the enemy’s ray.
I’m not sure what is happening when I destroy just one tank that is causing an error when I try to go destroy the second. I am not removing the ray or anything.
As far as the second issues goes -
When I drove the user_tank into the area where both radars overlap, the rays from both tanks were registering a collision with each others radar. And when I destroy both tanks, the rays are still being cast and it doesn’t call my onLevelCompletion function (which is nothing more than an overlay window with a level complete banner and a next button)
Is there a way to add my ray/s to a collision filter?
Here is a link to horacebury’s code -
link
* You can look at the firingsolutions.lua there.
Here is my entire fire function as well as an example of how I setup a radar:
[lua]
ET02radar = display.newCircle(enemyT02.x,enemyT02.y, 10)
ET02radar.isVisible = false      
physics.addBody(ET02radar,{radius = 400,isSensor = true, filter = RadarCollisionFilter})
ET02radar.class = “Radar”
– detect a target entering Etank02’s area and fire at it
local onET02radarCollision = function( self, event )
    
     if (event.phase == “began” and event.other.class == “PlayerTank”) then
         print(“began”)
        EnemyFiringTimer = timer.performWithDelay(500, function()
            ETfire( enemyT02, event.other )
        end, -1)
elseif (event.phase == “ended” and event.other.class == “PlayerTank”) then
        print(“collision with ET02radar ended.”)
        if EnemyFiringTimer then timer.cancel(EnemyFiringTimer) end
    end
    
    return true
    
end
ET02radar.collision = onET02radarCollision
ET02radar:addEventListener(“collision”, ET02radar)
[/lua]
Fire function:
[lua]
– fires enemy bullet
function ETfire( self, user_tank )
    
    EDray = physics.rayCast( self.x, self.y, user_tank.x, user_tank.y, “sorted” )
        
    local hitFirst = EDray[1]
    local hitX, hitY = hitFirst.position.x, hitFirst.position.y
    
    if EDray then
        – There’s at least one hit.
        print( "Hit count: " … tostring( #EDray ) )
        – Output all the results.
         for i,v in ipairs(EDray)  do
            print( "Hit: ", i, v.object.class, " Position: ", v.position.x, v.position.y, " Surface normal: ", v.normal.x, v.normal.y )
    end
        print( "The first object hit is: ",
        EDray[1].object.class, " at position: ", EDray[1].position.x, EDray[1].position.y, " where the surface normal is: ", EDray[1].normal.x, EDray[1].normal.y )
        else
        – There’s no hit.
    end
    if ( EDray[1].object.class == “PlayerTank” or EDray[1].object.class == “Uturret” or EDray[1].object.class == “bullet”) then
        
        print(“PlayerTank in range! Fire!”)
        timer.performWithDelay( 40, ShootEnemyBullet )
        
        function ShootEnemyBullet()
        – solution: “firing solution” - the collision point between the tower’s bullet and the target
        – success: true if the tower can successfully fire on the target, false if the target will escape
        local solution, success = intercept( self, user_tank, bulletSpeed )
                    –   
                local math_deg = math.deg  – localize ‘math.deg’ for better performance
                local math_atan2 = math.atan2  – localize ‘math.atan2’ for better performance
 
                – Calculates the angle between specified points A & B
                local function angleBetween( selfX, selfY, user_tankX, user_tankY )
                local angle = ( math_deg( math_atan2( user_tankY-selfY, user_tankX-selfX ) )+180) --; return angle
                    if ( angle < 0 ) then angle = angle + 360 end ; return angle % 360
                end
                local ang = angleBetween( self.x, self.y, user_tank.x, user_tank.y )
                self.rotation = ang
        
        – only fire if the firing solution is successful
        if (success) then
                – calculate vx and vy
                local angle = angleOf( self, solution )
                local pt = rotateTo( {x=bulletSpeed + 50, y=0}, angle)
                local pt2 = mathapi.rotateAboutPoint( {x=self.x,y=self.y -65}, self, self.rotation - 90 )
                
                – create bullet
                Enemybullet = loader:createSpriteFromSHDocument(“Weapon5ex”, “GameFX”, “LevelAssets.pshs”);
                Enemybullet.x, Enemybullet.y, Enemybullet.solution = pt2.x, pt2.y, solution
                physics.addBody(Enemybullet,{friction = 0, bounce = 1.0, density = 1.5, radius=10, isSensor = false, filter = bulletCollisionFilter})
                Enemybullet:scale(.6,.6)
                Enemybullet.class = “bullet”
                Enemybullet.isBulet = true
                Enemybullet.rotation = self.rotation + 90
                
                – fire bullet
                Enemybullet:setLinearVelocity(pt.x, pt.y)
                
                bullets:insert( Enemybullet )            
                Enemybullet.collision = onBulletCollision
                Enemybullet:addEventListener( “collision”, Enemybullet )
            end                
        end            
    end
end
[/lua]
[/lua]
Any help would be greatly appreciated. 
Edit: I changed a few things in the fire function. – code edited –
 
      
    
