C#XNA -Trying随机化对象列表的移动。除了一个以外,所有人都一样

时间:2014-07-09 17:38:36

标签: c# xna

我试图得到一个精灵对象列表,以短线性运动(向上,向下,向左,向右)在屏幕上随机移动。精灵随机移动,但它们都相同移动(例如,所有向左移动,向右移动等),除了分开移动的精灵。我希望他们都能独立地相互移动。以下代码 -

 namespace game2
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    /// <summary>
    /// Sprite
    /// </summary>
    List<Sprite> spriteList;
    Sprite tempSprite;
    private int spriteNum;


    /// <summary>
    /// General
    /// </summary>
    Random random;
    int randomNum;
    Rectangle viewPort;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        //Initialize Sprites
        spriteList = new List<Sprite>();
        spriteNum = 4;

        for (int i = 0; i < spriteNum; i++)  //Load in as many sprites as spriteNum specifies
        {
            tempSprite = new Sprite();
            spriteList.Add(tempSprite);      //add in sprites to list       
        }

        //initialise random
        random = new Random();

        //initialise viewport
        viewPort = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        foreach (Sprite s in spriteList)                    //for each sprite in spriteList, load content by passing in Game1's content
        {

            s.LoadContent(this.Content, viewPort, "square");
            s.Position = new Vector2(random.Next(0, viewPort.Width), random.Next(0, viewPort.Height));
        }

        // TODO: use this.Content to load your game content here
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();



        //update sprites

        foreach (Sprite s in spriteList)
        {

            s.Update(gameTime);
        }
        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);


        spriteBatch.Begin();


        foreach (Sprite s in spriteList)
        {
            s.Draw(this.spriteBatch);
        }


        spriteBatch.End();
        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }
}
}

这是精灵课 -

namespace game2
 {
class Sprite
{
    //Movement
    public Vector2 Position = new Vector2(0,0);
    private float counter = 0f;
    private float range = 100f;
    private Random random;
    public int randomNum;

    //Properties
    private Texture2D mSpriteTexture;

    //General
    private Rectangle viewPort;

    public void LoadContent(ContentManager theContentManager, Rectangle tempViewport, string asset)
    {
        random = new Random();

        viewPort = tempViewport;

        mSpriteTexture = theContentManager.Load<Texture2D>(asset);
    }

    public void Update(GameTime gameTime)
    {

        if (randomNum == 0)
        {

            if (Position.X + mSpriteTexture.Width < viewPort.Width && counter <= range)
            {
                    Position.X++;
                    counter++;
            }

            else
            {
                randomNum = random.Next(0, 4);
                counter = 0;

            }
        }

        if (randomNum == 1)
        {

            if (Position.X > viewPort.X && counter <= range)
            {
                Position.X--;
                counter++;
            }
            else
            {
                randomNum = random.Next(0, 4);
                counter = 0;

            }

        }

        if (randomNum == 2)
        {

            if (counter <= range)
            {
                Position.Y++;
                counter++;
            }
            else
            {
                randomNum = random.Next(0, 4);
                counter = 0;

            }

        }

        if (randomNum == 3)
        {

            if (counter <= range)
            {
                Position.Y--;
                counter++;
            }
            else
            {
                randomNum = random.Next(0, 4);
                counter = 0;

            }
        }
    }

    public void Draw(SpriteBatch theSpriteBatch)
    {
        theSpriteBatch.Draw(mSpriteTexture, Position, Color.White);
    }
}
}

1 个答案:

答案 0 :(得分:1)

您正在LoadContent正常加载所有精灵,这不是一个问题,但每个精灵都有自己的随机类

因此,许多人获得相同的种子(类似于在Randomfor循环中实例化while时看到的问题。具有相同种子的RNG将产生相同的值序列,从而导致您看到的行为。

将单个Random实例传递给使用它的所有对象会导致问题消失。