2011-12-30 59 views
0

所以,我正在做我自己的小行星游戏,我有点卡住了。当我按下按键Z时,我有一颗子弹从船上发射出去。但子弹总是以相同的方向发射,我不知道如何改变它?我希望它能够朝着船所面临的方向发射。此外,如果我快速推Z(发射许多子弹),一颗子弹似乎不断重置。我如何解决这些问题?这里是我的代码:试图射击子弹

class SpaceShip 
{ 
    private Vector2 pos; //Our position in 2D. 
    private Texture2D tex; //Our texture 
    private float speed = 8; //The speed to move. 
    private float angleOfRotation = 0; 
    Sprite tempSprite; 


    public SpaceShip(Vector2 pos, Texture2D tex, Texture2D bulletsToAdd) 
    { 
     this.pos = pos; //Our starting position 
     this.tex = tex; //Our texture 
     tempSprite = new Sprite(bulletsToAdd); 
    } 



    private void WorldWrap(int worldWidth, int worldHeight) 
    { 

     if (this.pos.X < 0) 
     { 
      //If we've past the left side of the window, set on other side of the world. 
      this.pos.X = worldWidth; 
     } 
     if (this.pos.X > worldWidth) 
     { 
      //If we've past the right side of the window, set on other side of the world. 
      this.pos.X = this.tex.Width; 
     } 
     if (this.pos.Y < 0) 
     { 
      //If we've passed the top side of the window, set on other side of the world. 
      this.pos.Y = worldHeight; 
     } 
     if (this.pos.Y > worldHeight) 
     { 
      //If we've passed the bottom side of the window, set on other side of the world. 
      this.pos.Y = this.tex.Height; 
     } 
    } 

    public void CreateBullets(Texture2D bulletsToAdd) 
    { 

     tempSprite.Position = new Vector2((pos.X),(pos.Y)); 
     tempSprite.Velocity = new Vector2((float)7, 7); 
     //tempSprite.Rotation = angleOfRotation; 
    } 


    public void Update(int WorldWidth, int WorldHeight, Texture2D bulletsToAdd) 
    { 
     KeyboardState keys = Keyboard.GetState(); 
     if (keys.IsKeyDown(Keys.A)) 
     { 
      //We want to turn the ship LEFT 
      this.angleOfRotation -= 2f; 

     } 
     if (keys.IsKeyDown(Keys.D)) 
     { 
      //We want to turn the ship RIGHT 
      this.angleOfRotation += 2f; 

     } 
     if (keys.IsKeyDown(Keys.W)) 
     { 
      //We're pressing the up key (W) so go Forwards! 
      this.pos.X += (float)(Math.Cos(this.angleOfRotation * Math.PI/180) * this.speed); 
      this.pos.Y += (float)(Math.Sin(this.angleOfRotation * Math.PI/180) * this.speed); 

     } 
     if (keys.IsKeyDown(Keys.S)) 
     { 
      //We're pressing the down key (S) so go Backwards! 
      this.pos.X -= (float)(Math.Cos(this.angleOfRotation * Math.PI/180) * this.speed); 
      this.pos.Y -= (float)(Math.Sin(this.angleOfRotation * Math.PI/180) * this.speed); 

     } 
     if (keys.IsKeyDown(Keys.Right)) 
     { 
      //We're pressing the up key (W) so go Forwards! 
      this.pos.X += (float)(Math.Cos(this.angleOfRotation * Math.PI/180) * this.speed); 
      this.pos.Y += (float)(Math.Sin(this.angleOfRotation * Math.PI/180) * this.speed); 

     } 
     if (keys.IsKeyDown(Keys.Left)) 
     { 
      //We're pressing the down key (S) so go Backwards! 
      this.pos.X -= (float)(Math.Cos(this.angleOfRotation * Math.PI/180) * this.speed); 
      this.pos.Y -= (float)(Math.Sin(this.angleOfRotation * Math.PI/180) * this.speed); 

     } 

     if (keys.IsKeyDown(Keys.Up)) 
     { 
      //We want to turn the ship LEFT. Rotate it counter-clockwise 
      this.angleOfRotation -= 2f; 

     } 
     if (keys.IsKeyDown(Keys.Down)) 
     { 
      //We want to turn the ship RIGHT. Rotate it clockwise 
      this.angleOfRotation += 2f; 

     } 
     if (keys.IsKeyDown(Keys.Z)) 
     { 

      CreateBullets(bulletsToAdd); 
     } 


     tempSprite.Position += tempSprite.Velocity; 

     //check if we need to move the ship. 
     WorldWrap(WorldWidth, WorldHeight); 

    } 

    public void Draw(SpriteBatch batch) 
    { 
     batch.Draw(this.tex, //Texture to use. 
     this.pos, //Position to draw at 
     new Rectangle(0, 0, this.tex.Width, this.tex.Height),Color.White, (float)((this.angleOfRotation + 90) * Math.PI/180), new Vector2(this.tex.Width/2, this.tex.Height/2), 1.0f, SpriteEffects.None, 0.0f); 

     batch.Draw(tempSprite.Texture, tempSprite.Position, null, Color.White, tempSprite.Rotation, tempSprite.Center, tempSprite.Scale, SpriteEffects.None, 1.0f); 
    } //End Draw function 

回答

3

首先,你只有一颗子弹 - tempSprite。您可能要创建一个单独的Bullet类和一个List<Bullet>某处,您可以添加项目符号。

所有你需要的是一个小三角。现在你的速度矢量是一个常数< 7,7>。你想要做的就是让这个矢量朝着太空船指向的方向前进。因此,假设您的角度是弧度而不是度(如果它们的角度是度数,则需要将它们更改为弧度),我们可以使用Math.Sin()Math.Cos()来获取速度矢量的各个X和Y分量,如下所示:

bulletSpeed = 7; 
tempSprite.Velocity = new Vector2((float)Math.Cos(angleOfRotation) * bulletSpeed, (float)Math.Sin(angleOfRotation) * bulletSpeed); 
+0

谢谢,这很有帮助。我想我会做一个单独的课程。有一个子弹列表将是很好的。我想我只是困惑,我将如何捕捉用户按下“Z”的次数......如果我把它放在一个列表中,列表将包含旧的和新的印刷机。不知道如何区分它们。我只是清除列表?还有一点呢?我怎么会知道哪一点做到这一点? – BigBug 2011-12-30 08:51:49

+0

在你的主要更新方法中,你可以遍历项目符号列表并删除所有不在屏幕外的项目符号,或者将更新方法添加到Bullet中,该项目记录了它自创建以来已经存在了多长时间,并删除了已经出现的项目符号超过x秒。每个Z按钮都会创建一个添加到列表中的子弹,不需要跟踪哪个子弹是哪个子弹,只需要有一些删除子弹的条件即可。 – 2011-12-30 09:00:20

+0

好吧,现在有道理。再次感谢 – BigBug 2011-12-30 09:04:04