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
謝謝,這很有幫助。我想我會做一個單獨的課程。有一個子彈列表將是很好的。我想我只是困惑,我將如何捕捉用戶按下「Z」的次數......如果我把它放在一個列表中,列表將包含舊的和新的印刷機。不知道如何區分它們。我只是清除列表?還有一點呢?我怎麼會知道哪一點做到這一點? – BigBug 2011-12-30 08:51:49
在你的主要更新方法中,你可以遍歷項目符號列表並刪除所有不在屏幕外的項目符號,或者將更新方法添加到Bullet中,該項目記錄了它自創建以來已經存在了多長時間,並刪除了已經出現的項目符號超過x秒。每個Z按鈕都會創建一個添加到列表中的子彈,不需要跟蹤哪個子彈是哪個子彈,只需要有一些刪除子彈的條件即可。 – 2011-12-30 09:00:20
好吧,現在有道理。再次感謝 – BigBug 2011-12-30 09:04:04