【问题标题】:Attempting to get asteroids to move down screen themselves试图让小行星自己向下移动屏幕
【发布时间】:2014-03-30 16:20:24
【问题描述】:

我正在尝试创建一个小行星游戏,而我正在尝试做的是让小行星自己在屏幕上向下移动。我的代码将小行星加载到屏幕上,但是小行星没有移动。

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

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
 
}
}

对于这个问题的任何帮助将不胜感激。

【问题讨论】:

  • 在你的 foreach 循环中,我看到你更新了一个位置,但它与每个 Asteroid a 有什么关系?
  • 不应该是foreach循环中的a.position,因为position是属于每个'Asteroids'对象的局部变量吗?
  • 尝试在 forach 循环中执行此操作,以便 foreach 循环读取 foreach (Asteroids a in asteroidsList) { a.position.X = a.position.X + speed} 我对y 位置,但小行星仍然不动

标签: c# xna


【解决方案1】:

您正在修改某物的位置,但我看不出修改该位置属性如何修改每个小行星的位置。您需要每颗小行星都有自己的位置属性。你可以尝试这样的事情(假设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 语句的需要。

【讨论】:

  • 刚刚添加了我的 game1.cs 代码,它是应该与 game1.cs 文件一起使用的代码的第一部分,或者在我首先放置的代码中
  • 嗯,这只是一个示例,说明如何使用其位置属性绘制每颗小行星。它应该进入Game1,但您可以轻松地将其修改为进入小行星类。基本上,看看你的代码,你的问题是每个小行星都没有自己的位置属性。
  • 当我尝试实现上面的代码时,不是在屏幕上输出 5 个小行星,而是只输出 1 个。
  • 如果您的抽奖有效,请继续使用它。但是,请尝试使用我编写的更新代码。这将为每个小行星分配一个位置值,而不是让它们都共享同一个。 (我上面有一个错误;我修复了它)
  • 不是真的,不是你的错,我不得不修改我的所有代码并将通用代码放入超类中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-13
  • 2018-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多