ArgumntNullException未处理 - 请帮助 [英] ArgumntNullException was unhandled - Please Help

查看:86
本文介绍了ArgumntNullException未处理 - 请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在编写一门编程课程并在其中一项作业中遇到问题。我有3个纹理(bear0,bear1,bear2),我打算随机调用它,基于0-2的随机生成。生成的值应告诉代码使用我的变量currentSprite分配一个
的纹理。但是,当我尝试运行我的程序时,currentSprite显示为具有空值,并且它会破坏程序。如果我在draw函数中用bear0替换currentSprite,程序就可以工作。

Hi, I am doing a coding course and have run into a problem in one of the assignments. I have 3 textures (bear0, bear1, bear2) which I intend to call upon randomly, based on a random generation of 0-2. The value generated should tell the code to assign one of the textures with my variable, currentSprite. however, when I attempt to run my program, currentSprite is showing as having null value, and it breaks the program. If I replace currentSprite with bear0 in the draw function, the program works.

我用粗体突出显示相关代码,但是我把所有我的代码,因为它可能在这一点上很好(我花了几个小时试图解决这个问题)。 

I have highlighted the relevant code in bold, however I put all my code in as it could very well be anything at this point (I have spent hours trying to fix this). 

我的代码(请注意,该程序尚未完成):

My code (note that the program is not finished):

使用System;

使用Microsoft.Xna.Framework;

using Microsoft.Xna.Framework;

推荐答案

我的猜测是问题就在这一行:

My guess would be that the issue is this line:

 int spriteNumber = rand.Next(0,3);

如果查看 Random.Next 您将看到它接受最小值和最大值设置为0和3.因此spriteNumber
将有1/4的机会被设置为3,即使你只有纹理0,1和2!

If you look at the documentation for Random.Next you will see it accepts a min value and a max value which you are setting to 0 and 3. So there is a 1 in 4 chance that spriteNumber will be set to 3, even though you only have textures 0, 1 and 2!

PS:你可能想要考虑将纹理放入Texture2D数组而不是3个单独的变量。然后你可以从数组中选出值并删除需要做一个大的"if(spriteNumber == 0)等等"。测试:)

PS: You may want to consider putting your textures into a Texture2D array rather than 3 separate variables. Then you can just pick out the value from the array and remove the need to do a big "if (spriteNumber==0) etc etc" test :)

currentSprite = bear[spriteNumber];


这篇关于ArgumntNullException未处理 - 请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆