0

我想要这种效果:

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

1 回答 1

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您可以尝试计算度数,将其存储为子弹的旋转值,然后完全摆脱。

于 2012-06-25T20:16:06.137 回答