AS3 - 如何 - 敌人对角射击子弹?

时间:2011-07-13 19:43:51

标签: flash actionscript-3 actionscript flash-cs5

我的代码指示敌人,当目标进入一定距离内时,敌人向目标射击子弹,向左/向下(对角线),向下或向右/向下(对角线)。 我的问题是:子弹射击很好但是,一旦目标不再在设定距离内 - 子弹曲线向下弯曲(y +)而不是继续它的初始轨迹。

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();
    }

我可以在代码中添加什么来确保子弹移动到其原始轨迹,无论目标移动到哪里?

感谢您的时间。

2 个答案:

答案 0 :(得分:2)

你的代码有点乱,想想这一秒:

子弹的轨迹何时定义?现在,你的子弹的行为就像一个指导代码破坏的导弹(没有违法行为:)。

特别是,由于这行代码,你的子弹进入y +:

y + = vy;

你可以看到它总是在循环中执行,即使x + = 5或-5不是......

你应该在子弹生命的开始时初始化你当前没有的vy和一些vx变量,即可能在子弹构造函数的某个地方

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;
              }
    }
}

我希望我在这段代码中没有犯任何错误......

另外,在我看来,你只希望子弹对角线到左下角或右下角......在这种情况下,请修改构造函数,使它只是像vy = 5;而不是所有这些:

if(target.y < y-20){
     vy = -5;
}else if(target.y > y+20){
     vy = +5;
} else {vy = 0;}

我希望这能帮到你。

答案 1 :(得分:0)

您需要创建一个新变量。称之为xspeed.Then使用看起来像这样的代码。

私有函数循环(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();
}

这样可以存储对象在x方向上移动的速度。然后无论目标是否在范围内,你总是以那个速度移动它。