试图让小行星自己向下移动屏幕

时间:2014-03-30 16:20:25

标签: c# xna

我试图创建一个小行星游戏,我想要做的是让小行星自己向下移动屏幕。代码我已经将小行星加载到屏幕上,但是小行星不会移动。

下面列出了我对小行星运动的代码。

public class Asteroids
{
 public int speed;

public Asteroids(Texture2D newTexture, Vector2 newPosition)
{
 speed = 2;
}
public LoadContent()
{
   while (asteroidsList.Count() < 5)
        {  
            randX = random.Next(0, 1000) + speed;
            randY = random.Next(-200, 984) + speed;
            asteroidsList.Add(new Asteroids(Content.Load<Texture2D>("asteroid big"), new Vector2(randX, randY)));
        }
 }

public void Update (GameTime gameTime)
{
  // Update Origin
        if (texture != null)
        {
            Asteroidorigin.X = texture.Width / 2;
            Asteroidorigin.Y = texture.Height / 2;
        }
        foreach (Asteroids a in asteroidsList)
        {
            position.Y = position.Y + speed;
            position.X = position.X + speed;
        }
        if (position.X >= 1280)
            {
            position.X = -105;
            }
        if (position.Y >= 1024)
            {
            position.Y = -105;
            }
}
}

Game1.cs
namespace AsteroidsGame
{
// Main
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Random random = new Random();

    // List Of Asteroids


    // Making New Objects Of These Classes
    Player p = new Player();
    Background bg = new Background();
    Asteroids a = new Asteroids();
    EnemySpaceship es = new EnemySpaceship();

    // Constructor
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        graphics.IsFullScreen = false; // Fullscreen mode
        graphics.PreferredBackBufferWidth = 1280; // Screen Width
        graphics.PreferredBackBufferHeight = 1024; // Screen Height
        this.Window.Title = "12013951 Asteroids Game";
        Content.RootDirectory = "Content";

    }

    // Init
    protected override void Initialize()
    {
        base.Initialize();
    }

    // Load Content
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        bg.LoadContent(Content);
        p.LoadContent(Content);
        a.LoadContent(Content);
        es.LoadContent(Content);
    }

    // Unload Content
    protected override void UnloadContent()
    {

    }

    // Update
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();


         bg.Update(gameTime);
        p.Update(gameTime);
        a.Update(gameTime);
        es.Update(gameTime);
        a.CheckCollisionAsteroid();

        base.Update(gameTime);
    }

    // Draw
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();

        bg.Draw(spriteBatch); //Draws The Background
        p.Draw(spriteBatch); // Draws The Player
        a.Draw(spriteBatch); // Draws asteroids
        es.Draw(spriteBatch); // Draws enemy spaceships

        spriteBatch.End();


        base.Draw(gameTime);
    }

    // Load Asteroids

}

} 对此问题的任何帮助将不胜感激。

干杯

1 个答案:

答案 0 :(得分:0)

你正在修改某些东西的位置,但我不知道如何修改该位置属性正在修改每个小行星的位置。你需要每个小行星都有自己的位置属性。您可以尝试这样的事情(假设position是小行星类的Vector2属性):

public void Update (GameTime gameTime)
{
    foreach (Asteroids a in asteroidsList)
    {
        a.position = new Vector2(
            MathHelper.Clamp(a.position.X + speed, -105, 1280),
            MathHelper.Clamp(a.position.Y + speed, -200, 1024));
    }
}

MathHelper.Clamp()也不再需要更新方法中的if语句。