如何使变量可用于 XNA/monogame 中的所有类? [英] How to make a variable available to all classes in XNA/monogame?

查看:65
本文介绍了如何使变量可用于 XNA/monogame 中的所有类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 XNA/Monogame Tower Defense 中有几个变量需要在类之间传递.不幸的是,它们中的许多是自动生成的,所以我不能只引用它们.有没有像我可以使用的全局变量之类的东西?这又是在 Monogame(XNA)/C# 中.

I have a couple of variables in my XNA/Monogame Tower Defense that I need to pass between classes. Unfortunately many of them are auto-generated so I can't just reference them in. Is there anything like a global variable that I can use? Again this is in Monogame(XNA) / C#.

推荐答案

只需创建一个静态类,您将在其中存储所有全局变量,并且所有类都可以访问它.

Just create a static class where you will store all your global variables and it will be accessible from all your classes.

public static class MyGlobals
{
    public static int LevelNumber { get; set; }
    public static string CurrentScore { get; set; }
    ...
}

并通过

public class AnotherClass
{
    public void SomeMethod()
    {
        MyGlobals.LevelNumber = ...
        string score = MyGlobals.CurrentScore;
    }
}

这篇关于如何使变量可用于 XNA/monogame 中的所有类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆