Linq FirstOrDefault逻辑

时间:2014-10-22 03:05:08

标签: c# linq

在我所拥有的方法中,我正在尝试将一个项目Tinder设置到一个火坑中并在我点击它时启动它。我遇到的问题是,如果我的库存中有多个火种,火就不会亮。如果我先点燃另一个火,我的库存中仍然有Tinder,那么大火就会点燃。除了我使用FirstOrDefault的第二行之外,我无法想到为什么会发生这种情况。

    private void StartFire()
    {
        MouseState currentMouseState = Mouse.GetState();

        //if player has more than one tinder in the inventory the fire won't start with click unless other fire is lit first

        if (player.NextToFirePit == true)
        {
            Item firestarter = player.PlayerInventory.Items.FirstOrDefault(i => i.ItemName == "firestarter");
            if (firestarter != null)
            {
                Item firepitwithtinder = allItemsOnGround.FirstOrDefault(i => i.ItemName == "firepitwithtinder" && i.ItemRectangle.Contains(MouseWorldPosition));
                if (firepitwithtinder != null && currentMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released)
                {
                    Fire fire = new Fire(new Vector2(firepitwithtinder.Position.X + (firepitwithtinder.Texture.Width / 2), firepitwithtinder.Position.Y + (firepitwithtinder.Texture.Height / 2)));
                    fire.FirePit = firepitwithtinder;

                    Fires.Add(fire);
                    player.PlayerLevelSystem.UpdateSkills(10, "firemaking");
                    firepitwithtinder.ItemName = "firepitwithfire";
                } oldMouseState = currentMouseState;
            }
        } 
    }

在这种情况下我没有使用FirstOrDefault吗?在火中设置火种工作正常,所以我不认为这种方法是问题。

0 个答案:

没有答案