gpt4 book ai didi

c# - 如何在另一个类(class)中使用一个类(class)的公共(public) boolean ? C#

转载 作者:太空宇宙 更新时间:2023-11-03 12:55:16 25 4
gpt4 key购买 nike

我正在尝试在类 Health 中使用类 Level 中的 boolean 值,但我无法让它工作。我知道这可能真的很微不足道,但现在我被困住了..

这是 Level 类中的 Uhealth 方法:

public bool UpHealth()
{
SpriteGameObject healthpack = this.Find("uphealth") as SpriteGameObject;
Player player = this.Find("player") as Player;
if (healthpack.CollidesWith(player))
return true;
return false;
}

这是 Health 类中我想使用 bool Uphealth 的方法:

public override void Update(GameTime gameTime)
{
base.Update(gameTime);
Player player = GameWorld.Find("player") as Player;
AnimatedGameObject rocket = GameWorld.Find("rocket") as AnimatedGameObject;

foreach (GameObject obj in gameObjects)
{
SpriteGameObject h = obj as SpriteGameObject;
if ( <---I DON'T KNOW WHAT TO TYPE HERE---> )
{
this.Add(h);
return;
}
}
}

我希望它在 Uhealth 为真时执行 this.Add(h);

我需要做什么才能在类 Health 中使用 Upehealth?

最佳答案

首先,创建类“Level”的实例。最好的方法可能是在类声明中(使其成为成员)。

Level level;

之后,只需确保它存在即可。

level = new Level(); // <------ I would probably put this in "LoadContent()"

最后,你可以这样使用它

if (level.UpHealth())
{
// Do really cool stuff here
}

希望对您有所帮助。

关于c# - 如何在另一个类(class)中使用一个类(class)的公共(public) boolean ? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34119180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com