仅列表的最后一个成员正在更新 [英] Only last made member of a list is updating

查看:66
本文介绍了仅列表的最后一个成员正在更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个游戏,其中以类似于Minecraft的样式添加了方块.最近,我在使用这些块时遇到了一些麻烦.

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

这是添加块时使用的代码

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;

           }


}

我尝试了以下

            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.

推荐答案

您正在测试每个带有电源模块的电源模块,并使用它来设置v.power.这意味着只有您测试的最后一个电源块才会对灯块产生影响.

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

这篇关于仅列表的最后一个成员正在更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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