AS3游戏角色只向一个方向射击

时间:2015-02-03 21:36:48

标签: actionscript-3 flash

我制作一个自上而下的射击游戏,到目前为止,我的角色可以移动,并且可以在点击鼠标时进行拍摄。但它只会向右射击而不是射向光标的位置。我该如何解决这个问题?

这里是主要课程中的项目符号代码:

      public var bulletList:Array = [];

      stage.addEventListener(MouseEvent.CLICK, shootBullet, false, 0, true);
      stage.addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

  public function shootBullet(e:MouseEvent):void
    {
        var bullet:Bullet = new Bullet(stage, harold.x, harold.y, harold.rotation);
        bullet.addEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved, false, 0, true); 
        bulletList.push(bullet);
        stage.addChild(bullet);
    }

    public function loop(e:Event):void 
    {
        if(bulletList.length > 0)
        {
            for(var i:int = bulletList.length-1; i >= 0; i--) 
            {
                bulletList[i].loop(); 
            }
        }
    }

    public function bulletRemoved(e:Event):void
    {
        e.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved); 
        bulletList.splice(bulletList.indexOf(e.currentTarget),1);
    }

以下是我的Bullet类中的代码:

   package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;

public class Bullet extends MovieClip
  {
    private var stageRef:Stage; 
    private var speed:Number = 10; 
    private var xVel:Number = 0; 
    private var yVel:Number = 0; 
    private var rotationInRadians = 0; 

    public function Bullet(stageRef:Stage, X:int, Y:int, rotationInDegrees:Number):void
    {
        this.stageRef = stageRef;
        this.x = X;
        this.y = Y;
    }

    public function loop():void
    {
        xVel = Math.cos(rotationInRadians) * speed; 
        yVel = Math.sin(rotationInRadians) * speed; 

        x += xVel; 
        y += yVel;


        if(x > stageRef.stageWidth || x < 0 || y > stageRef.stageHeight || y < 0)
            {
                this.parent.removeChild(this);
            }
    }
  }
}

2 个答案:

答案 0 :(得分:0)

我想你忘记在构造函数中指定rotationInRadians

this.rotationInRadians = rotationInDegrees * (Math.PI / 180);
顺便说一句,除非你希望子弹能够在飞行过程中改变方向,否则你不需要计算xVelyVel每个loop(),你可以计算它在构造函数中,存储它,然后更新每个x yloop()

答案 1 :(得分:0)

一些想法:

  1. 正如Aaron所说,你应该在构造函数中计算rotationInRadians,而实际的公式是this.rotationInRadians = rotationInDegrees * (Math.PI / 180);
  2. 您应该将rotationInRadians设为一个数字,即private var rotationInRadians:Number;
  3. 您是否知道displayObject有一个用于旋转的getter / setter?换句话说:harold.rotation是harold对象中的属性还是引用了getter?
  4. 我认为这就是它。