奇怪的循环行为? [英] Strange for loop behaviour?

查看:73
本文介绍了奇怪的循环行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Codeproject,



我目前正在为我的游戏添加盒子,它将充当碰撞系统。除了我遇到阵列问题外,这个工作正常。这是我的代码:



Hello Codeproject,

I am currently adding boxes to my game which will act as a collision system. This works fine except that I'm having problems with arrays. Here is my code:

/* Clear the bounding boxes. */
BoundingBoxes.Clear();

/* Loop through every block. */
for (int i = 0; i < ChunkWidth; i++)
{
    for (int j = 0; j < ChunkHeight; j++)
    {
        /* Create a new rectangle. */
        Tangle = new Rectangle(i * 32, j * 32, 32, 32);

        /* Add the rectangle. */
        if(Blocks[i,j].Material != BlockMaterial.Material_Air)
         BoundingBoxes.Add(Tangle);
    }
}









所以,ChunkWidth和Height都是20.所以你会期望它们最多运行19然后停止。并且19 * 32 = 608,但是在我的BoundingBox中没有单个矩形,X设置为608,并且只有18个矩形。



我一直在考虑,这是正常的,假设它从0开始而不是1,但在我的代码中,我的0和19都是数组列表,但由于它从未在列表中达到19,我不能使用cooardinate 608并且我的碰撞失败了。任何帮助?



编辑:

以下是调试时的列表:





So, ChunkWidth and Height are both 20. So you would expect them to run up to 19 and then stop. And 19 * 32 = 608, however in my BoundingBoxes there is no single rectangle that has the X set to 608, and there are only 18 rectangles.

Which I've been thinking about, and it's normal, assuming that it starts at 0 instead of 1, but in my code I have both 0 and 19 in my array list, but since it never reaches 19 inside the list I can't have the cooardinate 608 and my collision fails. Any help?


Here is the list when debugging:

Quote:

- BoundingBoxes Count = 19 System.Collections.Generic.List< microsoft.xna.framework.rectangle>

+ [0] { X:0 Y:32宽度:32高度:32} Microsoft.Xna.Framework.Rectangle

+ [1] {X:32 Y:64宽度:32高度:32} Microsoft.Xna。 Framework.Rectangle

+ [2] {X:64 Y:96宽度:32高度:32} Microsoft.Xna.Framework.Rectangle

+ [3] {X :96 Y:128宽度:32高度:32} Microsoft.Xna.Framework.Rectangle

+ [4] {X:128 Y:160宽度:32高度:32} Microsoft.Xna.Framework .Rectangle

+ [5] {X:160 Y:192宽度:32高度:32} Microsoft.Xna.Framework.Rectangle

+ [6] {X: 192 Y:224宽度:32高度:32} Microsoft.Xna.Framework.Rectangle

+ [7] {X:224 Y:256宽度:32高度:32} Microsoft.Xna.Framework。矩形

+ [8] {X:256 Y:288宽度:32高度:32} Microsoft.Xna.Framework.Rectan gle

+ [9] {X:288 Y:320宽度:32高度:32} Microsoft.Xna.Framework.Rectangle

+ [10] {X:320 Y:352宽度:32高度:32} Microsoft.Xna.Framework.Rectangle

+ [11] {X:352 Y:384宽度:32高度:32} Microsoft.Xna.Framework.Rectangle

+ [12] {X:384 Y:416宽度:32高度:32} Microsoft.Xna.Framework.Rectangle

+ [13] {X:416 Y :448宽度:32高度:32} Microsoft.Xna.Framework.Rectangle

+ [14] {X:448 Y:480宽度:32高度:32} Microsoft.Xna.Framework.Rectangle

+ [15] {X:480 Y:512宽度:32高度:32} Microsoft.Xna.Framework.Rectangle

+ [16] {X:512 Y: 544宽度:32高度:32} Microsoft.Xna.Framework.Rectangle

+ [17] {X:544 Y:576宽度:32高度:32} Microsoft.Xna.Framework.Rectangle

+ [18] {X:576 Y:608宽度:32高度:32} Microsoft.Xna.Framework.Rectangle

- BoundingBoxes Count = 19 System.Collections.Generic.List<microsoft.xna.framework.rectangle>
+ [0] {X:0 Y:32 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [1] {X:32 Y:64 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [2] {X:64 Y:96 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [3] {X:96 Y:128 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [4] {X:128 Y:160 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [5] {X:160 Y:192 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [6] {X:192 Y:224 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [7] {X:224 Y:256 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [8] {X:256 Y:288 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [9] {X:288 Y:320 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [10] {X:320 Y:352 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [11] {X:352 Y:384 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [12] {X:384 Y:416 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [13] {X:416 Y:448 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [14] {X:448 Y:480 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [15] {X:480 Y:512 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [16] {X:512 Y:544 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [17] {X:544 Y:576 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [18] {X:576 Y:608 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle







你可以看到没有[19],这是有道理的。但是我需要一个[19],但是当我例如使for循环< =时它会超出界限并检查它是什么类型的块,这也是正常的。



编辑:编辑:



再次考虑之后,没有第19个对象,因为0和19都在for循环。有什么建议?




As you can see there is no [19], which makes sense. But I need a [19] but when I for instance make the for loop "<=" it goes out of bounds with checking what type of block it is, that's normal too.



After thinking about it again, it makes no sense that there is no 19th object as that 0 and 19 are both in the for loop. Any suggestions?

推荐答案

如果你没有将一个矩形添加到具有608的X的BoundingBox,那么你需要查看你的Blocks数组,因为那样当i * 32 == 608,或i == 608/32,或者i == 19时,是唯一阻止添加矩形的东西,因为你已经检查了ChunkWidth并且确定它是20.



在其中加上else条款:

If you are not getting a rectangle added to the BoundingBoxes which has an X of 608, then you need to look at your Blocks array, because that is the only thing stopping a rectangle being added when i*32 == 608, or i == 608 / 32, or i == 19 given that you have checked ChunkWidth and are sure it is 20.

Put an "else" clause in there:
if(Blocks[i,j].Material != BlockMaterial.Material_Air)
   BoundingBoxes.Add(Tangle);
else
   {
   }

并在'{'上设置一个断点,这将确认它当调试器命中它并允许你检查数组内容时。



您可能应该使用Blocks.GetLength(0)和Blocks.GetLength(1)而不是ChunkWidth和ChunkHeight,或者至少检查它们是否在Blocks数组的限制范围内。

and put a breakpoint on the '{' - that will confirm it when the debugger hits it and allow you to check the array content.

You probably should be using Blocks.GetLength(0) and Blocks.GetLength(1) instead of ChunkWidth and ChunkHeight, or at least checking that they are within the limits of the Blocks array.


这篇关于奇怪的循环行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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