Only last made member of a list is updating

时光毁灭记忆、已成空白 提交于 2019-12-11 18:02:32

问题


I have a game in which blocks are added in a style similar to Minecraft. Lately, I've been running into a bit of a snag with these blocks.

There is one block called a power generator, and basically if it is on, and another block (light) is in the area, the light turns on. This is achieved through the following code, which is rather simple.

foreach (Block b in game.blphys.blocklist9)
{              
    foreach (Block v in game.blphys.blocklist7)
    {
        if (b.powerboxon == true && b.powerarea.Intersects(v.blockrectangle))
        {
            v.power = true;
        }
        else
        {
            v.power = false;
        }
    }
}

This has been working so far. However, since multiple generators can be added through listing, the moment I make a new power generator item the previously created power generator fails to work. The bounding box for the original generator is still there, and its still on (I have it change sprites while its on), it just looks like the code above stopped working for it.

Any help would be appreciated, I have a feeling I'm just being dumb about something

EDIT: Here is the code used while adding the block

if (game.player.Builder == true && game.player.LMBpressed == true && blockspawnamount >= placeblock)
{
           if (game.build.BlockID == 10 && game.menu.open == false)
           {

             position = new Vector2((int)(game.cursor.cursorPos.X / 58) * 58, (int)(game.cursor.cursorPos.Y / 58) * 58);

             game.blocktex9 = game.powerboxoff;
             block9 = new Block(game, game.blocktex9, new Vector2(position.X, position.Y), layer);
             blockpos9.Add(position);
             blocklayer9.Add(layer);
             blocklist9.Add(block9);
             placeblock = 200.0f;

           }


}

I tried the following

            foreach (Block v in game.blphys.blocklist7)
            {

                foreach (Block b in game.blphys.blocklist9)
                {                   

                    if (v.blockrectangle.Intersects(b.powerarea))
                    {
                        if (b.powerboxon == true)
                        {
                            v.power = true;
                        }
                        if (b.powerboxon == false) 
                        {
                            v.power = false;
                        }
                    }

                }

            }

Which somewhat relieves my problems. Now, if there are multiple generators in the area, one remains off, one remains on, if you add more the lights turn off, but you can turn on one of the generators and it works.

Adding generators out of the area works perfectly too, once turned on, they supply power, providing they are far enough away from the other generators.

I might just add some code which prevents you from building another generator within the same power area.


回答1:


You are testing every power block with your light blocks and using that to set v.power. This means that only the last power block you test will have an effect on the light block.

I'd recommend doing something like this instead:

iterate over light blocks
    default power to false
    iterate over power blocks
        if power block is on and close to light block, set power = true and stop iterating power blocks



回答2:


If the 2nd power generate is not near the 1st light, it will turn the power off. I suggest reversing the for each loops. If a light is near any power generator that is on, turn the light on and stop looking for power generators.

To explain further:

  1. power generator 1 is placed on the grid and added to the blocklist9 list.
  2. light 1 is placed near power generator 1.
  3. The code shown in your example shows that there is a power generator that is on and it is near light 1, so it turns the light on (v.power = true)
  4. power generator 2 is placed far away from light 1 (it's power area does not intersect)
  5. In the code show, power generator 1 turns the light on. Then, power generator 2 is determined to be too far away form light 1, so it turns the power back off (v.power = false;)


来源:https://stackoverflow.com/questions/25632050/only-last-made-member-of-a-list-is-updating

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!