私は自分の小惑星ゲームを作っています。私はちょっと立ち往生しています。私はキーを押したときに船から発射された弾丸を持っていますが、弾丸は常に同じ方向に発砲し、私はそれをどのように変更するのか分かりません。私はそれが船が向いている方向に発砲したい。また、Zを素早く(多くの弾丸を発射するために)押すと、1つの弾丸がリセットされ続けるように見えます。これらの問題を解決するにはどうすればよいですか?ここに私のコードは次のとおりです。 - tempSprite
弾丸を撃つことを試みている
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
メイン更新方法では、箇条書きリストを繰り返し、オフスクリーンのすべての箇条書きを削除するか、更新メソッドを「Bullet」に追加して、作成されてからの経過時間を追跡し、周囲にある箇条書きを削除することができますx秒以上。 Zキーを押すたびにリストに追加された箇条書きが作成されます。どの箇条書きを追跡する必要はなく、箇条書きを削除する条件が必要です。 –
さて、今、意味があります。もう一度おねがいします – BigBug