Flash AS3-如何-敌人斜向射击?

Flash AS3-如何-敌人斜向射击?,flash,actionscript-3,actionscript,flash-cs5,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) &

我的代码指示敌人,当目标在一定距离内时,敌人向目标发射子弹,要么左/下(对角线),要么右/下(对角线)。 我的问题是:子弹可以发射,但一旦目标不再在设定的距离内,子弹就会向下弯曲(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();
    }
私有函数循环(e:事件):无效
{
y+=vy;
x+=0;
x-=0;
if((target.x-x)>(target.y-y)&(getDistance(target.x,target.y,x,y)(target.x-x)&(getDistance(target.x,target.y,x,y)stageRef.stageWidth | | y<0)
removeSelf();
}
我可以在代码中添加什么来保持子弹在其原始轨迹中移动,而不管目标移动到哪里


谢谢您的时间。

您需要创建一个新变量。称之为xspeed,然后使用如下代码

私有函数循环(e:事件):无效 {

y+=vy;
if((target.x-x)>(target.y-y)&(getDistance(target.x,target.y,x,y)(target.x-x)&(getDistance(target.x,target.y,x,y)stageRef.stageWidth | | y<0)
removeSelf();
}

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

你的代码有点乱,请想一想:

子弹的轨迹何时确定?现在,你的子弹就像一枚制导代码被破坏的导弹(无意冒犯:))

具体来说,您的项目符号将进入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;
              }
    }
}
包{
公共类子弹扩展了MovieClip{
公共变量vx:编号;
公共变量:数量;
公共风险价值目标:MovieClip;
公共功能项目符号(目标:MovieClip,startX:Number,startY:Number){
/*让子弹知道这是个有点糟糕的主意
目标,但出于这个解释的目的,让我们这样做。
这应该放在子弹产生的地方,但现在*/
this.target=目标;
x=startX;
y=星形;
/*这是比循环稍微好一点的代码,并且只设置了一次
而不是一直*/
如果(目标xx+20){
vx=+5;
}else{vx=0;}
如果(目标yy+20){
vy=+5;
}else{vy=0;}
//现在我们创建循环本身
addEventListener(Event.ENTER_FRAME,循环);
}
私有函数循环(e:事件):无效{
x+=vx;
y+=vy;
/*这是冲突代码,请记住这可能也是
在创建项目符号和目标的类的enter帧中
子弹根本不应该知道目标,但唉,说来话长*/
如果(检查碰撞(目标)){
//做点什么
}
}
/*请注意,此函数也可以在此类之外使用。。。
这就是为什么我这样编程,而不是仅仅使用
这是一个测试*/
公共函数checkCollision(目标):布尔值{
if(本次hitTest(目标)){
返回true;
}
返回false;
}
}
}
我希望我没有在这段代码中犯任何错误

而且,在我看来,你只想让子弹斜向左下或右下。。。在这种情况下,请修改构造函数,使其具有类似vy=5的内容;而不是所有这些:

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

我希望这能为您带来好处。

这基本上是我已有的代码。虽然子弹保持一致的速度,但一旦离开目标区域,它仍然会“弯曲”回y+。我需要子弹继续它的轨迹(从它被射出的角度(右/下等)直到它退出屏幕。x+=0和x-=0有什么意义?非常感谢,恕我冒犯。我正在尝试在9月开始游戏开发学校之前尽可能多地学习3。你的代码看起来与我的代码非常相似,只是,我错误地将我的一些构造函数代码放入了循环中…我以为这就是应该的地方开始…我正在学习。卷积的距离码是我试图学习的一本书的剩余部分-你的(target.x>x+20)更加简单和优雅。我非常感谢你,你真的帮助了我。现在,我将尝试应用一些类似于我的播放器类的东西…我可能会带着更多的回来。。。
if(target.y < y-20){
     vy = -5;
}else if(target.y > y+20){
     vy = +5;
} else {vy = 0;}