XNA 3.5射击坦克炮

时间:2013-06-27 19:25:35

标签: c# xna

我有一个坦克身体(自上而下)的2D图像,可以在屏幕上左右移动。 最重要的是,坦克炮塔的第二个图像。用户鼠标沿Y轴移动后,该刀塔可以在屏幕边缘旋转。

当用户按下“输入”时,子弹出现并以炮塔的角度在屏幕上移动。然而,虽然角度很好,但子弹的位置似乎变化很大。有时,它位于它应该的位置(在大炮的中心)但是,当你移动鼠标时它似乎被抵消了。


编辑:由于一些奇怪的原因,我的照片似乎没有出现 - 所以这是一个直接链接:http://img824.imageshack.us/img824/7093/khte.png

编辑代码:

Tank Fire
     if (keyBoardState.IsKeyDown(Keys.Enter))
                {
                    shell.Initialize(rotation, new Vector2(location.X + 25, location.Y - 15));
                    shell.makeAlive();
                }

(它是用坦克的位置(+ 25,-25)初始化的,所以它出现在turrent的末尾。这个设置在坦克位置(shell.Initialize(旋转,位置);)似乎对偏移量没有影响。)

子弹/壳:

public void Update(GameTime gameTime)
    {
        if (alive)
        {
            movement.X -= speed * (float)Math.Cos(rotation);
            movement.Y -= speed * (float)Math.Sin(rotation);

            location.X += (int)movement.X;
            location.Y += (int)movement.Y;
        }
    }


    public void Draw(SpriteBatch spriteBatch)
    {
        if (alive)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            spriteBatch.Draw(texture, location, null, Color.White, rotation - MathHelper.PiOver2, new Vector2(texture.Width / 2, texture.Height / 2), 1.0f, SpriteEffects.None, 0f);
            spriteBatch.End();
        }
    }

3 个答案:

答案 0 :(得分:1)

如果你试图让你的子弹围绕你的坦克转动,那么我个人在坦克上方拔出子弹,就像你发射子弹后做的那样(但幅度更大) )取代它,

但是如果你想使用origin的{​​{1}}参数,那么我想它会看起来像这样:

给出以下数据点:

SpriteBatch.Draw

你还需要将子弹的位置初始化到适当的起点因为我实际上并不知道旋转的来源,我无法确定,但如果它是你的坦克和鼠标之间的角度,那么我想它会是以下

this.origin = new Vector2(texture.Width / 2, texture.Height / 2);
tank.origin = new Vector2(texture.Width / 2, texture.Height / 2);

// and this is what you'd pass into your sprite-batch.draw method
originToDraw = (tank.position + tank.origin)-(this.position + this.origin)

答案 1 :(得分:0)

我认为shellBullet个实例,如果是,您的问题是Bullet类的来源。它应该在LoadTextures方法

中设置此值
public void LoadTextures(Texture2D texture)
{
    this.texture = texture;
    this.origin = new Vector2(texture.Width, texture.Height);
}

Bullet.Draw方法中的额外内容:

spriteBatch.Draw(texture, location, null, Color.White, rotation - 1.5f, origin, 1.0f, SpriteEffects.None, 0f);

将1.5f更改为MathHelper.PiOver2当你想要旋转90度

时,它会更加适应

答案 2 :(得分:0)

这里有一个令人讨厌的结果问题。

您以浮点计算速度,但切换到子弹位置的整数。问题是,当 ON AVERAGE 时,即使任何给定的子弹出错,每次都会向上舍入或向下舍入。

让我们看一个极端的情况:子弹从垂直轴开出25度,每个更新周期移动两个像素。奇怪的是,子弹直接沿着轴线飞行。

或者更极端的情况。速度为每个周期1/3像素。子弹静止不动。