Understanding for-in-loop Calculs
I have this little code here, pixels fall from the top, if they collide with a block they will Stay on the surface of the block. Then if there is an other Pixel falling on the pixel that is no more falling it will add up. Well that is what I was trying to do, but after the second pixel add up开发者_JS百科, there is no more pixels that would get on top of the others. I though that j-1 is the current int in the index (- 1) so if it's 10, then it would be 9, so I'm a little confused why (int)Position[j - 1].Y - 1 doesn't work properly.
public void ParticleUpdate()
{
for (int j = 0; j < 1000; j++)
{
Position[j].Y += Gravity;
for (int u = 0; u < GlobalClass.BlocksPositions.Count; u++)
{
if (new Rectangle((int)GlobalClass.BlocksPositions[u].X, (int)GlobalClass.BlocksPositions[u].Y, 8, 8).Intersects(new Rectangle((int)Position[j].X, (int)Position[j].Y, 1, 1)))
{
Position[j].Y = (int)GlobalClass.BlocksPositions[u].Y - 1;
//This code works perfectly.
}
}
if (j - 1 > 0)
{
if (new Rectangle((int)Position[j - 1].X, (int)Position[j - 1].Y, 1, 1).Intersects(new Rectangle((int)Position[j].X, (int)Position[j].Y, 1, 1)))
{
Position[j].Y = (int)Position[j - 1].Y - 1;
//Here is the problem.
}
}
}
}
Also I have some lag issues if I put a lot of blocks is there a way to optimize this?
精彩评论