So I was able to incorporate horacebury’s code, but I’m getting a weird response when my user_tank collides with my enemy tank’s invisible sensor (radar).
Here is a short vid of what is happening: video
* For some reason the video quality on dropbox is terrible, but if you download the video it should be HD.
Here is my code for the enemy tank, fyi all the objects without local preceding them have a forward declaration:
[lua]
local enemyB02 = loader:spriteWithUniqueName(“Ebase2”)
enemyB02.class = “EnemyTank02”
local enemyT02 = loader:spriteWithUniqueName(“Eturret2”)
enemyT02.class = “EnemyTank02”
------------Adding a Radar for Etank02
--------
ET02radar = display.newCircle(enemyT02.x,enemyT02.y, 10)
ET02radar.isVisible = false
physics.addBody(ET02radar,“static”,{radius=400,isSensor=true})
– detect a target entering the object’s area and fire at it
function ET02radar:collision(e)
if (e.phase == “began” and e.other.class == “Player”) then
print(“began”)
timer.performWithDelay(1, function()
fire( ET02radar, e.other )
end, 1)
end
return true
end
ET02radar:addEventListener(“collision”, ET02radar)
– fires bullet
function fire( tower, user_tank )
– 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( tower, 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( enemyB02X, enemyB02Y, user_tankX, user_tankY )
local angle = ( math_deg( math_atan2( user_tankY-enemyB02Y, user_tankX-enemyB02X ) )+180) --; return angle
if ( angle < 0 ) then angle = angle + 360 end ; return angle % 360
end
local ang = angleBetween( enemyB02.x, enemyB02.y, user_tank.x, user_tank.y )
enemyT02.rotation = ang
–
– only fire if the firing solution is successful
if (success) then
– calculate vx and vy
local angle = angleOf( tower, solution )
local pt = rotateTo( {x=bulletSpeed, y=0}, angle)
local pt2 = mathapi.rotateAboutPoint( {x=enemyT02.x,y=enemyT02.y -65}, enemyT02, enemyT02.rotation - 90 )
– create bullet
Enemybullet = loader:createSpriteFromSHDocument(“Weapon5ex”, “GameFX”, “LevelAssets.pshs”);
Enemybullet.x, Enemybullet.y, Enemybullet.solution = pt2.x, pt2.y, solution
--bullet.x = pt2.x; bullet.y = pt2.y;
physics.addBody(Enemybullet,{friction = 0, bounce = 1.0, density = 1.5, radius=10, isSensor = true})
Enemybullet:scale(.65,.65)
Enemybullet.class = “bullet”
Enemybullet.isBulet = true
Enemybullet.rotation = enemyT02.rotation + 90
– fire bullet
--bullet:applyForce( pt2.x - enemyT02.x, pt2.y - enemyT02.y, enemyT02.x, enemyT02.y )
Enemybullet:setLinearVelocity(pt.x, pt.y)
end
end
[/lua]
Also, I want the bullets to react the physical environment, but in doing so causes my enemy tank fire bullets towards the player even if there is an obstacle between them.
In adition to what I have already, should I add my original method of firing an invisible bullet and if it collides only with the user then tell it to fire a real bullet Or would that be the wrong to go about things?