如何在另一个班级中使用一个班级的公共布尔? C#

时间:2015-12-06 15:35:34

标签: c# class xna boolean

我试图在课堂上使用Level等级的bool,但是我无法让它工作。我知道这可能真的很微不足道,但现在我被困了......

以下是来自班级的Uphealth方法:

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;
        }
    }
}

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

如果能够在Health类中使用Uphealth,我需要做什么?

1 个答案:

答案 0 :(得分:1)

首先,创建一个类&#34; Level&#34;的实例。最好的方法是在类声明中(使其成为成员)。

Level level;

之后,只需确保它存在。

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

最后,你可以这样使用它

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

我希望我有所帮助。