I finally got my game running on a device, and now I am working on refining certain things, as best I can.
The thing I’m having trouble with is my ‘enemy firing timer’ setup.
It works, but not quite… as good as I’d like.
I’d appreciate any input on how I could achieve the following -
I have a timer being triggered when the playing collides with an enemy’s radar.
Basically when the user collides with an enemy’s radar, the enemy starts
casting rays towards the user and if the first hit of ray is the user then the enemy fires a bullet.
The problem is that the firing “delay” is constant and I’d prefer to have a bit of randomness, to give the illusion (however minor) of Ai.
I thought adding math.random(500, 2500) would do just that, but unfortunately it picks a random value (obviously between the specified values) and that is the constant rate of fire rather than on each iteration changing the delay.
If an enemy casts a ray and it hits the player, I want them to instantly fire a bullet towards the player, rather than the delay be set for the time between the enemy cast the ray and calls the function to fire a bullet.
Here is my radar collision function:
local onEradarCollision = function( self, event ) if (event.phase == "began" and event.other.class == "PlayerTank") then EnemyFiringTimer = timer.performWithDelay(math.random(500, 2500), function() ETfire( tank.turret, event.other ) end, -1) elseif (event.phase == "ended" and event.other.class == "PlayerTank") then print("collision with tank.radar radar ended.") if EnemyFiringTimer then timer.cancel(EnemyFiringTimer) end end return true end tank.radar.collision = onEradarCollision tank.radar:addEventListener("collision", tank.radar)
– the ETfire function is responsible for casting a ray towards the user and then firing a bullet if the first hit was the player. –
I mean, I understand that when the user hits the radar is triggers the timer which chooses a value between, in this case, 500, 2500.
Obviously it doesn’t change because I’m not hitting the radar over-an-over calling that function.
My question is how can I code it to where the EnemyFiringTimer’s delay is constantly changing between the specified values?
hehe I hope I made enough sense… I’ve been staring at my code all day and it’s starting to get to me. 
-Saer
Edit: Say I collide with three radars at the same time, each enemy will get a random value between 500, 2500 but whatever value it chosen will be the constant delay rather than changing.
I know it’s not constantly changing because I’m only colliding with the radar once.
I’m just trying to figure out to have the delay constantly change.
