Finding map height in pixels?

Aug 2, 2011 at 12:25 AM

I have multiple scenes in my game I'll be making. I noticed that:

map = Content.Load<Map>("Maps\\Map01");

doesn't work in anything other than Game1.cs... Is there a way to make it work in my scene called gamePlayScene?

Aug 2, 2011 at 1:32 AM

Some more searching around and i figured it out. Sorry to bother you all.

Aug 2, 2011 at 7:29 AM

Okay I was wrong... Anyone have a tutorial on using this with multple scenes? I have a main menu, character creation, then the game play scene. Shouldn't as long as i follow the tutorial but put it in my GamePlayScreen.cs shouldn't it still work? Or am i doing something wrong cause i cant get the maps to load on a different scene all night...

Aug 2, 2011 at 10:16 AM

When you say it doesn't work, what sort of problem is it exactly? Do you get an exception, and if yes, can you please post the details?

Aug 3, 2011 at 11:15 PM
Edited Aug 4, 2011 at 5:52 AM

I figured it out. I was trying to draw the map inside of my drawing for my animated sprite. I got it working... Now i only need to fix how my sprite moves. I'm following tutorials for an rpg over at and for their camera they get the height of the map in pixels... Is there a way to find the height in pixels from this map editor through code.

I'm using map.DisplayWidth right now for thats the only thing i can really find for the width then im using displayHeight for the height... I have the character moving... Now does any one have a suggestion on how to move the viewport along with the character?

Aug 4, 2011 at 8:46 AM

Yes map.DisplayHeight gives you the height in pixels. If the map has multiple layers, it will give you the height of the largest layer (heightwise).


As for centering the viewport on your character, which is at position PX, PY say, you basically need to compute the viewport top left corner VX, VY as VX = PX - VW / 2 and VY = PY - VH / 2 where VW and VH are the viewport's width and height.. these usually correspond to your chosen display resolution.

Aug 4, 2011 at 9:04 AM

Thanks for the help. I'll change the coding tomorrow. now its time to get some sleep. Been up and working on this and a few other improvements since about 8 this morning...

Also I'm loving this map editor. I keep finding more and more features I love by just messing around with it. I can't wait till i get my game up and running.

Aug 4, 2011 at 7:34 PM

I try what you posted but for some reason i think i'm doing it wrong. When i put that equation in then it limits to to up and down once i hit the middle of the map. So i know im getting closer. Now i just need to get the up and down to be limited then figure out how to scroll the map..... Thanks for all your help.

Aug 4, 2011 at 9:19 PM

Why don't you have a look at the code of the demo application? You can browse online into the project directly (, then go into TileMapEditor/Demo/DemoGame.cs.

Aug 4, 2011 at 9:25 PM
Edited Aug 4, 2011 at 9:48 PM

Thanks for the link. I'll go do that now.


Well i found the code I most likely should be looking at which is


// limit viewport to map
            m_viewPort.Location.X = Math.Max(0, m_viewPort.X);
            m_viewPort.Location.Y = Math.Max(0, m_viewPort.Y);
            m_viewPort.Location.X = Math.Min(
                m_map.DisplayWidth - m_viewPort.Width, m_viewPort.X);
            m_viewPort.Location.Y = Math.Min(
                m_map.DisplayHeight - m_viewPort.Height, m_viewPort.Y);
Now i just need to get it to work with my LockCamera() or my LockToSprite()... Havent figured that one out yet. Time to do some testing with that along with the example you gave.