2013-06-27 28 views
0

我有一个坦克车身(自上而下)的2D图像,可以在屏幕上左右移动。在顶部,还有坦克炮塔的第二个图像。用户沿着Y轴移动鼠标后,该刀塔可以在屏幕边缘上旋转。XNA 3.5射击坦克炮

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


编辑:出于某种奇怪的原因,我的图片似乎并没有被显示出来 - 所以这里是一个直接链接: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(rotation,location);)中设置该值似乎对偏移没有影响)

子弹/壳牌:

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

你是什么意思,你不能让子弹定位在炮塔的位置?在你解雇他们之后,还是在你解雇他们之前,鼠标是否会让他们移动? –

+0

嗨,请看我的更新。我已经截取了子弹的位置(用黑色标记)和我想要的位置(用黄色标记) 谢谢 – user1662290

+0

您是否想过只在子弹顶部产生子弹然后在您的子弹中取代子弹更新循环,如果它还没有解雇? –

回答

1

如果你想让你的子弹旋转你的坦克,比我亲自在坦克的顶部画子弹做的事情完全一样,你做你的子弹后(但与更大的UDE)来取代它,

但是,如果你想使用的SpriteBatch.Draworigin参数,比我想象它会是这个样子:

给出以下数据点:

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) 

你也必须将子弹的位置初始化为合适的起点由于我实际上并不知道旋转的来源,所以我无法确定,但如果它是你的坦克和鼠标之间的角度, d想象它会是以下

this.startingPosition = tank.position + tank.origin - this.origin 
+0

嗨, 这个想法是旋转它周围的起始点,以便它将被绘制在当前的末尾...我现在编辑了代码并添加了一张新图片。它仍然没有排队...... – user1662290

+0

谢谢......它现在都在工作:D – user1662290

0

我asume是shellBullet例如,如果是你的问题是在Bullet类的起源。

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

变化1.5F到MathHelper.PiOver2当你想要的东西旋转90度

更accure:应该在 LoadTextures方法

public void LoadTextures(Texture2D texture) 
{ 
    this.texture = texture; 
    this.origin = new Vector2(texture.Width, texture.Height); 
} 

而且在Bullet.Draw方法一点点额外设置了此值

+0

嗨, 我已经做了这个改变,谢谢你。 和是的,bullet = shell,对不起,我会让它少混乱! 代码现在更新虽然 – user1662290

0

这里有一个讨厌的舍入问题。

你计算浮点速度,但切换到整数的子弹的位置。问题是,尽管任何特定的子弹都会出现错误,但是每次都会舍入或舍入。

让我们来看一个极端的例子:子弹射出垂直轴25度,每更新周期移动两个像素。奇怪的是,子弹直接飞向轴线。

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