我正在尝试模拟同时发射多发子弹的枪(类似于展开射击)。我在想我必须创建另一个子弹阵列,然后像下面一样做同样的事情,但方向不同。
这是我到目前为止所拥有的:
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