我正在尝试创建一种方法来在触发使用 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();
}
}
}