0

我正在尝试模拟同时发射多发子弹的枪(类似于展开射击)。我在想我必须创建另一个子弹阵列,然后像下面一样做同样的事情,但方向不同。

这是我到目前为止所拥有的:

foreach (GameObject bullet in bullets) 
{
    // Find a bullet that isn't alive
    if (!bullet.alive)
    {
        //And set it to alive
        bullet.alive = true;

        if (flip == SpriteEffects.FlipHorizontally) //Facing right
        {
            float armCos = (float)Math.Cos(arm.rotation - MathHelper.PiOver2);
            float armSin = (float)Math.Sin(arm.rotation - MathHelper.PiOver2);

            // Set the initial position of our bullets at the end of our gun arm
            // 42 is obtained by taking the width of the Arm_Gun texture / 2
            // and subtracting the width of the Bullet texture / 2. ((96/2)=(12/2))
            bullet.position = new Vector2(arm.position.X + 42 * armCos, arm.position.Y + 42 * armSin);

            // And give it a velocity of the direction we're aiming.
            // Increae/decrease speed by changeing 15.0f
            bullet.Velocity = new Vector2(
                (float)Math.Cos(arm.rotation - MathHelper.PiOver4 + MathHelper.Pi + MathHelper.PiOver2),
                (float)Math.Sin(arm.rotation - MathHelper.PiOver4 + MathHelper.Pi + MathHelper.PiOver2)) * 15.0f;
        }

        else //Facing left
        {
            float armCos = (float)Math.Cos(arm.rotation + MathHelper.PiOver2);
            float armSin = (float)Math.Sin(arm.rotation + MathHelper.PiOver2);

            //Set the initial position of our bullet at the end of our gun arm
            //42 is obtained be taking the width of the Arm_Gun texture / 2
            //and subtracting the width of the Bullet texture / 2. ((96/2)-(12/2))
            bullet.position = new Vector2(arm.position.X - 42 * armCos, arm.position.Y - 42 * armSin);

            //And give it a velocity of the direction we're aiming.
            //Increase/decrease speed by changing 15.0f
            bullet.Velocity = new Vector2(-armCos, -armSin) * 15.0f;
        }
        return;
    }// End if
}// End foreach
4

1 回答 1

2

一种简单的方法是使用 for 循环并将旋转度数增加一个固定量。

这是一个伪代码示例。

var spawnPoint = new Vector2(x, y);

for (int angle = 45; angle <= 135; angle += 45)
{
    Bullet.ShootInDirection( spawnPoint, MathHelper.ToRadians(angle) );
}

45°这将在、90°和处发射 3 发子弹135°。您可以相应地调整这些值,以便以不同的角度增量或多或少地拍摄。

这是一个视觉表示。

视觉表现

供将来参考,专门用于游戏开发的堆栈交换网站,当您在那里发布这些类型的问题时可能会有更好的结果。

于 2013-11-12T01:09:30.663 回答