Forum Thread
  Posts  
Loading Map with multiple classes (Forums : Coding & Scripting : Loading Map with multiple classes) Locked
Thread Options
Mar 2 2013 Anchor

I am having a problem with my game. The problem is mainly centered around loading the map. The way I have things set up is each level resides in it's own class. Each level also contains it's own map. My problem is the maps do not load properly. For example, the map loaded in Level 2 will load in level 1. I hope I can get some answers, and thanks in advance for any solutions. The source is available on my GIT repo: Source

Thanks, -DanqueDynasty

Edited by: DanqueDynasty

Mar 4 2013 Anchor

The attributes in the class BlockMap are all static:

//initialize the global variables
    public static TiledMap tmap;
    public static int mapWidth;
    public static int mapHeight;

This means they are only once in the memory for the entire class/application and not bound to one object.
Then in the constructor of MainGame you do:

    public MainGame(String name)
    {
        super(name);
        this.addState(new HomeMenu(HOME_STATE));
        this.addState(new mazeLevel(MAZE_STATE));
        this.addState(new TestLevel(TEST_LEVEL));
    }

You load the level mazeLevel where you create a BlockMap object and then TestLevel where you "create another" BlockMap object, but because in both parts you are using the same static attributes of BlockMap the class TestLevel overwrites the mazeLevel.

The BlockMap-objects in the level-classes are also static.
Try to remove all the static-keywords, they are only useful for (class-)global values.

Best regards
- Martin

Edited by: MausGames

Mar 4 2013 Anchor

Did that and it works, but now there is a problem with the collision detection. Thanks^^

Mar 12 2013 Anchor

Whats the problem with collision detection?

Mar 12 2013 Anchor

Hehe, forgot to mention that I resolved it^^ Basically I had to rewrite my method for collision detection to work abstractly. Everything works now thanks.

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.