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

标签 c# class xna boolean

我正在尝试在类 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/

相关文章:

c# - asp.net中如何调用JS函数

c# - 在函数中设置可选参数需要常量表达式错误

c# - 禁用 vs2010 XNA 内容管道警告

c# - 如何从其他 C# 项目调用 VSTO 类

c# - ASP.Net Core JWT token 验证

python - 如何使用具有多重继承的数据类特殊方法?

php - 扩展数据库类以从 MySql 获取结果

c++ - 在类计时器中重载前缀和后缀增量。调试时出现问题

visual-studio - Visual Studio 2010 中的 SL/XNA 解决方案的不同项目应该包含哪些内容?

objective-c - 是否有与 XNA 的更新功能等效的 Objective-C?