How to place loaded TileMap at screen center?

  • Hi all!
    I have been struggling with setting my tile maps at the center of the screen. Currently, all my tiles map were loaded onto different location of my screen, and I am unsure why (some levels will have upper left corner at (0, 0), some will start at the center of the screen, etc.).
    Since all of my maps are of different dimension, I wish to find a way to get the width of the map and adjust other components like HUD accordingly, and maintain the correct entities offset when the map is moved to the center......

    I have tried using .screenCenter() on my map, which does something totally weird.
    I also tried to get the width of the background layer of my levels, but I don't know if there is a function to set the position of FlxTileMap.

    Any insights are super appreciated!!

  • Try this:

    tileMap.setPosition((FlxG.width - tileMap.width) / 2, (FlxG.height - tileMap.height) / 2);

  • @DleanJeans Thanks for your response. However, the code only shifted all of my entities to the center, and the tile map (wall layer) of my game did not get moved at all??

  • Are you using a camera and scrolling? If you use the camera you shouldn't need to move the tilemaps, unless you're doing something I'm not understanding.

    You won't have to keep adjusting the hud position either. You can use myHudObject.scrollFactor.set(); for your hud objects and they won't scroll. The hud will always stay where you put it, but as your player moves the screen will scroll everything else.

    Maybe some clarification will help.

  • also, try setting worldBounds.
    FlxG.worldBounds.set(0, 0, map.width, map.height);

  • thank you all! I fixed the issue by setting using camera.setScale().

Log in to reply

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.