函数不会在 Bonfire 游戏引擎中将变量设置回 false

function will not set variable back to false in the Bonfire game engine

我正在尝试创建一种方法,当使用 Bonfire 游戏引擎的传感器被触发时在地图上拾取对象,并通过我编写的使用游戏更新功能的函数将这些项目放入我的库存中查看对象是否被拾起。我遇到一个问题,当我拿起物品并将拾取值设置为 true,然后将其存储在我的一个物品槽变量中,然后再将其设置回 false 时,它​​不会返回 false,因为函数我写信是想看看它是否被捡起,但由于某种原因无法将布尔值设置回去,然后继续将该项目复制到我的其余项目槽中。这是我创建的对象以及游戏更新函数和我编写的用于检查项目是否被拾取的函数。

检查变量是否为真的函数

  checkMagicHitForEachBox(bool itemBooleanValue, String magicName) {
    if (itemBooleanValue == true) {
      if (bottomMaigcItemSlot == null) {
        setState(() {
          itemBooleanValue = false; // This is what is broken! It will not set MagicHeart.heart back to false.
          bottomMaigcItemSlot =
              magicName; // Sets the bottom item slot to the item I picked up
        });
      } else if (topMagicItemSlot == null) {
        setState(() {
          itemBooleanValue = false; // This is what is broken! It will not set MagicHeart.heart back to false.
          topMagicItemSlot =
              magicName; // Sets the top item slot to the item I picked up
        });
      }
    } else if (topAttackItemSlot != null) { // Tells me if both are already full
      print('full');
      setState(() {
        widget.magicFull = true;
      });
    }
    print(itemBooleanValue);
  }

每毫秒更新一次的函数

  @override
  void updateGame() {
    checkMagicHitForEachBox(MagicHeart.heart, 'magic_heart.png');

我创建的对象

class MagicHeart extends GameDecoration with Sensor {
  static bool heart = false;
  InterfaceOverlay interfaceOverlay = InterfaceOverlay();
  MagicHeart(Vector2 position)
      : super.withAnimation(
          SpriteAnimation.load(
            "magic/magic_heart.png",
            SpriteAnimationData.sequenced(
              amount: 1,
              stepTime: 0,
              textureSize: Vector2(16, 16),
            ),
          ),
          width: 16,
          height: 16,
          position: position,
        );

  @override
  void onContact(GameComponent component) {
    if (component is Player && interfaceOverlay.magicFull != true) {
      heart = true;
      removeFromParent();
    }
  }
}

您正在 checkMagicHitForEachBox 内部更改原始数据类型,itemBooleanValue 只会在方法范围内更改,不会在方法范围外更改。您可以通过传入拥有 itemBooleanValue 的父项来解决该问题。

如果您想在多个 classes 上使用这样的值,我建议您创建一个包含布尔值的 mixin,并且您可以在多个 classes 上添加.

不过现在您似乎只使用 MagicHeart class,所以您可以像这样发送 class:

  checkMagicHitForEachBox(MagicHeart magicHeart, String magicName) {
    if (magicHeart.heart == true) {
      if (bottomMaigcItemSlot == null) {
        setState(() {
          magicHeart.heart = false;
          bottomMaigcItemSlot =
              magicName; // Sets the bottom item slot to the item I picked up
        });
      } else if (topMagicItemSlot == null) {
        setState(() {
          magicHeart.heart = false;
          topMagicItemSlot =
              magicName; // Sets the top item slot to the item I picked up
        });
      }
    } else if (topAttackItemSlot != null) { // Tells me if both are already full
      setState(() {
        widget.magicFull = true;
      });
    }
  }

最后但同样重要的是,我认为您不希望 heart 变量是静态的,因此只需从中删除静态变量:static bool heart = false; -> bool heart = false;