首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XNA Texture2D列表outOfRangeException

XNA Texture2D列表outOfRangeException
EN

Stack Overflow用户
提问于 2013-11-23 20:59:47
回答 1查看 186关注 0票数 0

首先,我是xna的新手。尝试抓取21点游戏。我已经创建了2个列表来添加随机卡到列表中。但是我不明白为什么不把图片添加到列表tekstuur2中

代码如下:

代码语言:javascript
复制
public class Kaart
    {

        public Vector2 asukoht = new Vector2(0, 0);

        public List<Texture2D> tekstuur = new List<Texture2D>();
        public List<Texture2D> tekstuur2 = new List<Texture2D>();

        Random  rand = new Random();
        public void loadContent2(ContentManager manager2)
        {



            for (int x = 3; x < 7; x++)

                tekstuur2.Add(manager2.Load<Texture2D>("Risti" + x.ToString()));

        }

        public void loadContent(ContentManager manager)
        {

            for (int j = 3; j < 7; j++)



                tekstuur.Add(manager.Load<Texture2D>("Risti" + j.ToString()));

        }

        public void Draw(SpriteBatch sprite)
        {

            sprite.Draw(tekstuur[rand.Next(tekstuur.Count)], asukoht, Color.White);
            sprite.Draw(tekstuur2[rand.Next(tekstuur2.Count)], asukoht, Color.White); 
// an error occurs (ArgumentOutofRangeException) , tekstuur2.count = 0 at that point, but at the tekstuur.Count it is 4.

        }

    }
}

下面是另一个调用这些方法的类:

代码语言:javascript
复制
namespace WindowsGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Vector2 koht = new Vector2(0,0);

    Kaart yks;
    Kaart kaks;


    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
        yks = new Kaart();
        kaks = new Kaart();

        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);

        //kaardi laadimine 
        yks.loadContent(this.Content);
        kaks.loadContent2(this.Content);


        // yhe kaarti asukoht
        yks.asukoht.X = 100;
        yks.asukoht.Y = 300;
        // teise kaardi asukoht
        kaks.asukoht.X = 200;
        kaks.asukoht.Y = 400;



        // 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();



        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)
    {
        // kaartide joonistamine 
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        yks.Draw(this.spriteBatch);
        kaks.Draw(this.spriteBatch);

        spriteBatch.End();

        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }
}

}

EN

回答 1

Stack Overflow用户

发布于 2013-11-23 21:19:08

好的,问题是你应该为Kaart的每个实例(在你的例子中是yks和kaks)调用loadContent,并在你的方法LoadContent中调用loadContent2:

代码语言:javascript
复制
    //kaardi laadimine 
    yks.loadContent(this.Content);
    kaks.loadContent2(this.Content);

成为:

代码语言:javascript
复制
    //kaardi laadimine 
    yks.loadContent(this.Content);
    yks.loadContent2(this.Content);
    kaks.loadContent2(this.Content);
    kaks.loadContent(this.Content);

请注意,由于纹理是相同的,因此不需要使用两个不同的列表来包含它们,您可以在Kaart Draw方法中执行此操作:

代码语言:javascript
复制
sprite.Draw(tekstuur[rand.Next(tekstuur.Count)], asukoht, Color.White);
sprite.Draw(tekstuur[rand.Next(tekstuur.Count)], asukoht, Color.White); 

这样,您只需调用LoadContent。

另一个注意事项:目前您正在实例化一个随机数;问题是不推荐连续实例化这个类(在短时间内),并且您的两个KArt将使用相同的随机纹理。要解决此问题,请将Random rand设置为静态变量:

代码语言:javascript
复制
private static Random  rand = new Random();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20162583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档