My code instructs the Enemy that when the Target gets within a certain distance that the Enemy fires a bullet at the Target, either left/down(diagonal), down, or right/down(diagonal).
My problem is: the Bullet fires fine but, once the Target is no longer within the set distance – the Bullets curves to down(y+) rather than just continuing on it’s initial trajectory.
private function loop(e:Event) : void
{
y += vy;
x += 0;
x -= 0;
if ((target.x - x) > (target.y - y) && (getDistance(target.x, target.y, x, y) < interestDistance / 2))
x += 5;
if ((target.y - y) > (target.x - x) && (getDistance(target.x, target.y, x, y) < interestDistance / 2))
x -= 5;
if (y > stageRef.stageWidth || y < 0)
removeSelf();
}
What can I add to the code to kees the Bullet moving in it’s original trajectory regardless of where the Target has moved to?
Thank you for your time.
Your code is a slight mess, think about this for a second:
When is the trajectory of the bullet defined? Right now, your bullet is acting like a guided missile with broken guidance code (no offense :)).
Specifially, your bullet is going into y+ because of this line of code:
y += vy;
as you can see it is always executed inside the loop even when x+=5 or -5 is not…
The vy and some vx variable you currently do not have should be initialized at the start of the bullets life, ie probably somewhere in the bullets contructor
I hope I did not make any errors in this code…
Also, it seems to me you only want the bullet to go diagonally to the lower left or lower right… in that case, please modify the constructor so that it simply has something like vy = 5; instead of all this:
I hope this does the trick for you.