AS3 - How To - Enemy Shoot Bullet Diagonally?
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 t开发者_如何学Gohe 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
package {
public class Bullet extends MovieClip{
public var vx:Number;
public var vy:Number;
public var target:MovieClip;
public function Bullet(target:MovieClip, startX:Number, startY:Number){
/*this is a slightly bad idea to let the bullet have any knowledge of
the target but for purposes of this explanation lets do it like that.
This should better be put where the bullet was created but for now...*/
this.target = target;
x = startX;
y = startY;
/*this is slightly better code then your loop and it is only set once
instead of all the time*/
if(target.x < x-20){
vx = -5;
}else if(target.x > x+20){
vx = +5;
} else {vx = 0;}
if(target.y < y-20){
vy = -5;
}else if(target.y > y+20){
vy = +5;
} else {vy = 0;}
//now we create the loop itself
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event) : void{
x+=vx;
y+=vy;
/*this is collision code, bear in mind this should probably also be
in an enter frame in the class that created the bullet and the target
the bullet simply should not know about the target but alas, long story...*/
if(checkCollision(target)){
//do something
}
}
/* notice that this function can be used outside of this class as well...
this is why i programmed it like this instead of just using the
this.hitTest if*/
public function checkCollision(target):Boolean{
if(this.hitTest(target)){
return true;
}
return false;
}
}
}
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:
if(target.y < y-20){
vy = -5;
}else if(target.y > y+20){
vy = +5;
} else {vy = 0;}
I hope this does the trick for you.
You need to make a new variable. Call it xspeed.Then use code that looks like this.
private function loop(e:Event) : void {
y += vy;
if ((target.x - x) > (target.y - y) && (getDistance(target.x, target.y, x, y) < interestDistance / 2))
xspeed = 5;
if ((target.y - y) > (target.x - x) && (getDistance(target.x, target.y, x, y) < interestDistance / 2))
xspeed = -5;
x+=xspeed;
if (y > stageRef.stageWidth || y < 0)
removeSelf();
}
This way you store how fast the object was moving in the x direction. Then you always move it that speed regardless of if target is in range or not.
精彩评论