Thanks for replying.
I use object.class for my objects.
I changed object_name to v.object.class and it now prints the name of whatever object the ray collides with.
Sadly, using the code you suggested for my ray’s conditional statement, it still won’t fire even when the object.class is PlayerTank…?
-You’ll notice, in the code below, right after the ray’s if statement I have
print( “PlayerTank in range! Fire!” )
When I run the simulator, and the only object.class that hits the radar is PlayerTank, the enemy won’t fire…?
I don’t get any errors and it doesn’t print the statement I just mentioned.
Obviously it’s colliding with and registering the PlayerTank, but it won’t go beyond that if statement :huh:
[lua]
function fire( tower, user_tank )
E1Dray = physics.rayCast( enemyB02.x, enemyB02.y, user_tank.x, user_tank.y, “closest” )
local hitFirst = E1Dray[1]
local hitX, hitY = hitFirst.position.x, hitFirst.position.y
DRline = display.newLine( enemyB02.x, enemyB02.y, user_tank.x, user_tank.y )
DRline.width = 10
DRline:setColor( 255,50,40,100 )
DRline.blendMode = “add”
level1group:insert( DRline )
if E1Dray then
– There’s at least one hit.
print( "Hit count: " … tostring( #E1Dray ) )
– Output all the results.
for i,v in ipairs(E1Dray) 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: ",
E1Dray[1].object, " at position: ", E1Dray[1].position.x, E1Dray[1].position.y, " where the surface normal is: ", E1Dray[1].normal.x, E1Dray[1].normal.y )
else
– There’s no hit.
end
if ( E1Dray[1].class and E1Dray[1].class == “PlayerTank” ) then
print(“PlayerTank in range! Fire!”)
– 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
end
[/lua]
P.S. When I comment-out the if statement and the closing end, it fires whenever the user collides with the radar; so I know the code/logic for the actual firing works fine.