代码仅在最后一个数组元素上循环

时间:2018-02-07 20:29:34

标签: c# monogame

请考虑以下代码:

 protected override void Update(GameTime gameTime)
    {
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        for(int x = 0; x < 2; ++x)
        {
            if (isTouchingDown(player.playerRectangle, gameObject.CollisionShape.rectangles[x]))
            {
                player.CanGoUp = false;
            }
            else { player.CanGoUp = true; }

            if (isTouchingUp(player.playerRectangle, gameObject.CollisionShape.rectangles[x]))
            {
                player.CanGoDown = false;
            }
            else { player.CanGoDown = true; }

            if (isTouchingRight(player.playerRectangle, gameObject.CollisionShape.rectangles[x]))
            {
                player.CanGoLeft = false;
            }
            else { player.CanGoLeft = true; }

            if (isTouchingLeft(player.playerRectangle, gameObject.CollisionShape.rectangles[x]))
            {
                player.CanGoRight = false;
            }
            else { player.CanGoRight = true; }

        }


        player.Update();

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();

        spriteBatch.DrawString(font, ("rect1.Top:" + gameObject.CollisionShape.rectangles[0].Top), new Vector2(10, 10), Color.Black);
        spriteBatch.DrawString(font, ("rect1.Bottom:" + gameObject.CollisionShape.rectangles[0].Bottom), new Vector2(10, 25), Color.Black);
        spriteBatch.DrawString(font, ("rect1.Left:" + gameObject.CollisionShape.rectangles[0].Left), new Vector2(10, 40), Color.Black);
        spriteBatch.DrawString(font, ("rect1.Right: " + gameObject.CollisionShape.rectangles[0].Right), new Vector2(10, 55), Color.Black);

        spriteBatch.DrawString(font, ("rect2.Top:" + gameObject.CollisionShape.rectangles[1].Top), new Vector2(170, 10), Color.Black);
        spriteBatch.DrawString(font, ("rect2.Bottom:" + gameObject.CollisionShape.rectangles[1].Bottom), new Vector2(170, 25), Color.Black);
        spriteBatch.DrawString(font, ("rect2.Left:" + gameObject.CollisionShape.rectangles[1].Left), new Vector2(170, 40), Color.Black);
        spriteBatch.DrawString(font, ("rect2.Right:" + gameObject.CollisionShape.rectangles[1].Right), new Vector2(170, 55), Color.Black);

        spriteBatch.DrawString(font, ("player.Top:" + player.playerRectangle.Top), new Vector2(330, 10), Color.Black);
        spriteBatch.DrawString(font, ("player.Bottom:" + player.playerRectangle.Bottom), new Vector2(330, 25), Color.Black);
        spriteBatch.DrawString(font, ("player.Left:" + player.playerRectangle.Left), new Vector2(330, 40), Color.Black);
        spriteBatch.DrawString(font, ("player.Right:" + player.playerRectangle.Right), new Vector2(330, 55), Color.Black);
        gameObject.Draw(spriteBatch);

        Texture2D t = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
        t.SetData<Color>(new Color[] { Color.Yellow });

        spriteBatch.Draw(t, gameObject.CollisionShape.rectangles[0], Color.White);
        spriteBatch.Draw(t, gameObject.CollisionShape.rectangles[1], Color.White);

        player.Draw(spriteBatch);
        spriteBatch.End();

        base.Draw(gameTime);
    }
基本上,我有一个玩家,而且我有一个对象。

每个对象都有他的普通矩形(用于绘图的纹理区域) 和碰撞矩形(或矩形组)。

在这个例子中,我有一个树的对象,所以我的碰撞矩形组中需要2个矩形。一个矩形用于树的树干,一个矩形用于树的树枝。

无论如何一切都有效,正如你所看到的,我能够看到所有三个矩形的信息(玩家矩形和一棵树的2个矩形)并且我填充了树的2个碰撞矩形以在游戏中看到它们。

现在问题是,当我 for 循环更新()时 看起来像这样:

for(int x = 0; x < 1; ++x) //rest of that for loop up here

然后只检查碰撞内容的第一个碰撞矩形而不是第二个(这对我来说很清楚,因为x总是0)

BUT

如果我的for循环像这样:

for(int x = 0; x < 2; ++x) //rest of that for loop here

然后只有我的树碰撞组的第二个矩形被追逐,而第一个矩形没有...为什么第一个没有在这里被追查:///?

1 个答案:

答案 0 :(得分:1)

你最后一个循环的结果是剩下的,因为你没有组合你的布尔值但是设置为绝对。这些:

hour<-rep(0:24, 1828)
day_of_seq<-rep(0:1827, 25)

library(dplyr)
df<-data.frame(hour, day_of_seq)%>%
 mutate(year = ifelse((day_of_seq > 0 & day_of_seq <= 366), 2004,
               ifelse((day_of_seq >= 337 & day_of_seq <= 731), 2005,
               ifelse((day_of_seq >= 732 & day_of_seq <= 1096), 2006,
               ifelse((day_of_seq >= 1097 & day_of_seq <= 1461), 2007,
               ifelse((day_of_seq >= 1462 & day_of_seq <= 1827), 2008, "nope")))))) 

应该是:

if (isTouchingDown(player.playerRectangle, gameObject.CollisionShape.rectangles[x]))
    player.CanGoUp = false;
else { player.CanGoUp = true; }

if (isTouchingUp(player.playerRectangle, gameObject.CollisionShape.rectangles[x]))
    player.CanGoDown = false;
else { player.CanGoDown = true; }

if (isTouchingRight(player.playerRectangle, gameObject.CollisionShape.rectangles[x]))
    player.CanGoLeft = false;
else { player.CanGoLeft = true; }

if (isTouchingLeft(player.playerRectangle, gameObject.CollisionShape.rectangles[x]))
    player.CanGoRight = false;
else { player.CanGoRight = true; }

应该是&#34; addiditive&#34;因此,如果第一个if (isTouchingDown(player.playerRectangle, gameObject.CollisionShape.rectangles[x])) player.CanGoUp = false; else { player.CanGoUp &= true; } // == player.CanGoUp = Player.CanGoUp && true // ^^^^ if false to begin with, will stay false if (isTouchingUp(player.playerRectangle, gameObject.CollisionShape.rectangles[x])) player.CanGoDown = false; else { player.CanGoDown &= true; } // same here and below if (isTouchingRight(player.playerRectangle, gameObject.CollisionShape.rectangles[x])) player.CanGoLeft = false; else { player.CanGoLeft &= true; } if (isTouchingLeft(player.playerRectangle, gameObject.CollisionShape.rectangles[x])) player.CanGoRight = false; else { player.CanGoRight &= true; } 设置为false,则无法在第二个x上覆盖它,并再次为true。

为了他的工作,你必须从:

开始
x
player.CanGoUp = true; player.CanGoDown = true; player.CanGoLeft = true; player.CanGoRight = true; 循环之前

如果所有内容都启动为真 - 您也可以完全删除for

else