java - How to remove static variable but allow a variable to still be globally called? -
i using libgdx scene2d , tiled create 2d game. have class reads object layers , loads them game(which still deciding whether make singleton). problem in class gets updated, since split update , render 2 classes, have public static stage stage
, want make not static.
the problem thaat have mob class, includes players , monsters, , need stage class in order of objects in game , check collisions.
in mob class don't think/want add stage constructor parameter because it's abstract class , have put in stage player class every time called.
if additional information needed, because know guys aren't wizards, put asap. have 3 classes working here, tiledmaphelper class, worldcontroller class, , mob class.
below class converts created level on tiled codeable objects game. static arraylists changed group class implemented stage class. stage class holds of objects in game.
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(); (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(); (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(); (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); } } } } } }
denfeet, hope i'm not being presumptuous when know few ways deal problem , you're asking recommendation best one. if that's case, answer may bit more "general tips" helpful, can wholeheartedly recommend singleton main game class project. yes, avoid if tried. question - worth time (especially when you're trying learn library)? static main game class out lot of trouble you'll still fall while creating project , learning libgdx - , won't take ages rewrite (i suppose i'm saying - learn mistakes , save time spent figuring out hard way :-)).
big trap young players way android handles static instances - recreate them onresume avoid it. happy coding.