【问题标题】:How to remove static variable but allow a variable to still be globally called?如何删除静态变量但仍允许全局调用变量?
【发布时间】:2016-06-02 01:39:14
【问题描述】:

我正在使用带有 Scene2D 和 Tiled 的 LibGDX 来创建 2d 游戏。我有一个类可以读取对象层并将它们加载到游戏中(我仍在决定是否将其制作成单例)。问题是在所有内容都更新的类中,因为我喜欢将更新和渲染分成两个类,所以我有一个public static Stage stage,我想让它不是静态的。

thaat 的问题是我有一个 Mob 类,其中包括 玩家和怪物,我需要舞台课程才能获得所有 游戏中的对象并检查碰撞。

在我的 Mob 类中,我不认为/不想将 Stage 作为参数添加到构造函数中,因为它是一个抽象类,每次调用播放器类时我都必须为它放入一个舞台。

如果需要任何其他信息,因为我知道你们不是巫师,我会尽快提出。我在这里使用三个类,TiledMapHelper 类、WorldController 类和 Mob 类。

下面是将我在 Tiled 上创建的关卡转换为我的游戏的可编码对象的类。静态 ArrayLists 将被更改为一个 Group 类,该类被实现到 stage 类中。 Stage Class 包含我游戏中的所有对象。

public class TiledMapHelper {
    public static final int TILE_WIDTH = 512;
    public static final int MAP_WIDTH = 39 * TILE_WIDTH;
    public static final int MAP_HEIGHT = 45 * TILE_WIDTH;`

    public static ArrayList<Platform> platforms;
    public static ArrayList<Wall> walls;
    public static ArrayList<Door> doors;
    public static ArrayList<Stair> stairs;
    public static ArrayList<Ladder> ladders;

    private TiledMap tiledMap;

    public TiledMapHelper(TiledMap tiledMap) {
        this.tiledMap = tiledMap;

        initWalls();
        initGates();
        initPlatforms();
    }

    public void initWalls() {
        walls = new ArrayList<Wall>();
        MapObjects layerObjects = tiledMap.getLayers().get("walls").getObjects();
        for (MapObject mapObject : layerObjects) {
            if (mapObject instanceof RectangleMapObject) {
                Rectangle rect = ((RectangleMapObject) mapObject).getRectangle();
                Wall wall = new Wall(rect.x, rect.y, rect.width, rect.height);
                walls.add(wall);
            }
        }
    }

    public void initGates() {
        doors = new ArrayList<Door>();
        stairs = new ArrayList<Stair>();
        ladders = new ArrayList<Ladder>();
        MapObjects layerObjects = tiledMap.getLayers().get("gates").getObjects();
        for (MapObject mapObject : layerObjects) {
            if (mapObject instanceof TiledMapTileMapObject) {
                if (mapObject.getName() == null)
                    continue;
                if (mapObject.getName().equals("Door")) {
                    int checkDoorType = Integer.parseInt((String) mapObject.getProperties().get("doorType"));
                    if (checkDoorType == 1) {
                        Door door = new Door(((TiledMapTileMapObject) mapObject).getX(),
                                ((TiledMapTileMapObject) mapObject).getY(), "door1C");
                        doors.add(door);
                    }
                    if (checkDoorType == 2) {
                        Door door = new Door(((TiledMapTileMapObject) mapObject).getX(),
                                ((TiledMapTileMapObject) mapObject).getY(), "door2C");
                        doors.add(door);
                    }
                }
            }
            if (mapObject instanceof RectangleMapObject) {
                Rectangle rect = ((RectangleMapObject) mapObject).getRectangle();
                if (mapObject.getName().equals("Stair")) {
                    String checkStairType = (String) mapObject.getProperties().get("isStair");
                    if (checkStairType.equals("upRight")) {
                        Stair stair = new Stair(rect.x, rect.y, rect.width, rect.height, "upRight");
                        stairs.add(stair);
                    }
                    if (checkStairType.equals("upLeft")) {
                        Stair stair = new Stair(rect.x, rect.y, rect.width, rect.height, "upLeft");
                        stairs.add(stair);
                    }
                    if (checkStairType.equals("downRight")) {
                        Stair stair = new Stair(rect.x, rect.y, rect.width, rect.height, "downRight");
                        stairs.add(stair);
                    }
                    if (checkStairType.equals("downLeft")) {
                        Stair stair = new Stair(rect.x, rect.y, rect.width, rect.height, "downLeft");
                        stairs.add(stair);
                    }
                }
                if (mapObject.getName().equals("Ladder")) {
                    String checkLadderType = (String) mapObject.getProperties().get("isLadder");
                    if (checkLadderType.equals("up")) {
                        Ladder ladder = new Ladder(rect.x, rect.y, rect.width, rect.height, "up");
                        ladders.add(ladder);
                    }
                    if (checkLadderType.equals("down")) {
                        Ladder ladder = new Ladder(rect.x, rect.y, rect.width, rect.height, "down");
                        ladders.add(ladder);
                    }
                }
            }
        }
    }

    public void initPlatforms() {
        platforms = new ArrayList<Platform>();
        MapObjects layerObjects = tiledMap.getLayers().get("platforms").getObjects();
        for (MapObject mapObject : layerObjects) {
            if (mapObject instanceof TiledMapTileMapObject) {
                if (mapObject != null) {
                    String checkPlatformType = (String) ((TiledMapTileMapObject) mapObject).getTile().getProperties().get("platformType");
                    if (checkPlatformType.equals("left")) {
                        Platform platform = new Platform(((TiledMapTileMapObject) mapObject).getX(),
                                ((TiledMapTileMapObject) mapObject).getY(), "left");
                        platforms.add(platform);
                    }
                    if (checkPlatformType.equals("middle")) {
                        Platform platform = new Platform(((TiledMapTileMapObject) mapObject).getX(),
                                ((TiledMapTileMapObject) mapObject).getY(), "middle");
                        platforms.add(platform);
                    }
                    if (checkPlatformType.equals("right")) {
                        Platform platform = new Platform(((TiledMapTileMapObject) mapObject).getX(),
                                ((TiledMapTileMapObject) mapObject).getY(), "right");
                        platforms.add(platform);
                    }
                }
            }
        }
    }
}

【问题讨论】:

    标签: java static libgdx scene2d tiled


    【解决方案1】:

    Denfeet,当我说您已经知道一些解决问题的方法并且您正在寻求最佳方法的建议时,我希望我不是自以为是。如果是这种情况,这个答案可能比通常有用的更“一般提示”,但我只能全心全意地为您的项目推荐一个单例 ma​​in game 类。是的,如果你尝试过,你可以避免它。问题是——值得你花时间(尤其是当你试图学习图书馆的时候)?一个静态的主游戏课程会让你摆脱在创建项目和学习 libgdx 时仍然会遇到的很多麻烦——而且重写所有东西不会花费你很长时间(我想我是说——从我的错误并节省我花在解决这个问题上的时间:-))。
    对于年轻玩家来说,唯一的大陷阱是 Android 处理静态实例的方式 - 在 onResume 上重新创建它们以避免它。编码愉快。

    【讨论】:

    • 你说得对,我确实给自己一些解决方案。我想我只是在组织我的代码时遇到了麻烦。一切都很顺利,直到我开始添加碰撞。我花了大约两周时间才找到添加它的正确方法,而且我已经厌倦了仅仅为了做一个小改动就撕掉我的代码。这就是导致这个问题的原因,我必须为这个小静态变量更改一大堆代码。谢谢你的回复,我自己看看能不能解决。
    【解决方案2】:

    Singleton 对象似乎可以解决您的问题,因为您可以将实例变量包装在静态方法调用后面。不过,我不确定您要解决什么问题。你能发布你的代码吗?

    编辑: 单例 Stage 的基本结构如下所示:

    public class Stage{
    
        //Stage's fields
    
        //keep the constructor private
        private Stage(){
    
        }
    
        //Stage's methods
    
        private static class StageHolder { 
           //private field of the one Stage object
           private static final Stage INSTANCE = new Singleton();
        }
    
        //public static message to access the one Stage
        public static Stage getInstance() {
           return StageHolder.INSTANCE;
        }
    }
    

    想法是将Stage 的实例隐藏为私有静态字段,并在需要访问Stage 时使用getInstance() 方法。

    【讨论】:

    • 好的,我会发布将Tiled地图级别转换为编码对象的类。
    • 我已经发布了代码,静态 Stage 变量在我的 WorldController 类中。如果你能帮我把代码改成单例就好了。
    • 您也可以发布Stage 课程吗? @WonderfulWorld 使用单例主游戏类的想法只要您在开发时将其保持在较小的一侧,就会奏效。
    • 如果singleton 函数是静态的,那么它访问的SINGLETON 成员也应该是静态的。我的一个问题是,您为什么偏离了您链接到的“Java 中的单例”页面?该站点上的示例没有更大且线程安全。
    • 是的!我在尝试手动重新创建它而不引用我链接的站点时犯了一个错误。已进行更正。
    【解决方案3】:

    为它所在的类创建一个构造函数,并使用访问器方法来引用它

    【讨论】:

    • 是的,我刚刚编辑了问题,我忘了说我不想这样做。谢谢你。
    【解决方案4】:

    每个暴徒都有自己的特定阶段吗?

    我认为stage有createMob方法是可以的,它将获取配置mob并创建mob。您可以让 mob 构造函数具有 stage 参数,但无需在每次创建类时都传递 stage。

    例如:

    abstract class Stage {
        public Mob createMob(MobConfiguration config) {
            ... // your mob creation method
            if (config.type == MobConfiguration.TYPE_PLAYER) {
                return new Player(this, config);
            }
        }
    }
    class Player extends Mob {
        public Player(Stage stage, MobConfiguration config) {
            // some initialization method
        }
    }
    

    或者您可以将createMob 方法放到拥有舞台的其他类中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 2022-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      相关资源
      最近更新 更多