以360度差距拍摄多发子弹?

时间:2012-06-25 18:07:17

标签: actionscript-3 actionscript

我想要这种效果:

http://i.imgur.com/xGi46.png

因此,无论我点击哪里,它都会使用子弹制作炸弹类型。 到目前为止,这是我的代码。现在它只在鼠标方向上创建子弹。 对不起,如果代码很乱。

shotDex = new Timer(timerDelay2);
shotDex.addEventListener(TimerEvent.TIMER, shot);

stage.addEventListener(MouseEvent.MOUSE_DOWN, shootBullet);
stage.addEventListener(MouseEvent.MOUSE_UP, dontShoot);

    public var angleRadian = Math.atan2(mouseY + 42.75,mouseX + 331.7);
    public var angleDegree = angleRadian * 180 / Math.PI;
    public var speed1:int = 10;

    public var shotDex:Timer;
    public var timerDelay2:int = (250);
    public function shot(tEvt:TimerEvent)
    {
        var _bullet2:bullet2 = new bullet2;
        _bullet2.x = 300;
        _bullet2.y = 300;
        _bullet2.angleRadian = Math.atan2(mouseY + 42.75,mouseX + 331.7);
        _bullet2.addEventListener(Event.ENTER_FRAME, bulletEnterFrame);
        stage.addChild(_bullet2);
    }
    public function shootBullet(evt:MouseEvent)
    {
        var _bullet2:bullet2 = new bullet2;
        _bullet2.x = 300;
        _bullet2.y = 300;
        _bullet2.angleRadian = Math.atan2(mouseY + 42.75,mouseX + 331.7);
        stage.addChild(_bullet2);
        _bullet2.addEventListener(Event.ENTER_FRAME, bulletEnterFrame);
        shotDex.start();
    }
    public function bulletEnterFrame(evt:Event)
    {
        var _bullet2 = evt.currentTarget;
        _bullet2.x +=  Math.cos(_bullet2.angleRadian) * speed1;
        _bullet2.y +=  Math.sin(_bullet2.angleRadian) * speed1;
        _bullet2.rotation = _bullet2.angleRadian * 180 / Math.PI;
        if (_bullet2.x < 0 || _bullet2.x > 600 || _bullet2.y < 0 || _bullet2.y > 600)
        {
            stage.removeChild(_bullet2);
            _bullet2.removeEventListener(Event.ENTER_FRAME, bulletEnterFrame);
        }
    }
    public function dontShoot(evt:MouseEvent)
    {
        shotDex.stop();
    }

1 个答案:

答案 0 :(得分:2)

您需要为每个项目符号提供不同的角度,其值均匀分布在0弧度和2 * Math.PI弧度之间:

public function shootBulletCircle(evt:MouseEvent) {
    var shots:Number = 12; // Number of shots in the circle
    for (var i=0; i<shots; i++) {
        var _bullet2:bullet2 = new bullet2;
        _bullet2.x = 300;
        _bullet2.y = 300;
        _bullet2.angleRadian = (i/shots)*(2*Math.PI);
        stage.addChild(_bullet2);
        _bullet2.addEventListener(Event.ENTER_FRAME, bulletEnterFrame);
    }
}

作为可选的旁注:DisplayObject(可能是您的Bullet s的超类)具有rotation属性,在绘制时会自动使用,但它需要该值以度为单位。您可以尝试计算度数,将其存储为子弹的旋转值,并完全摆脱angleRadian