Troubleshooting this TutorialSometimes, even though you try hard to understand the information in a tutorial, things don't work out quite like you want it to. This page is here to help you resolve any problems you might be having with the tutorial on changing the window size in XNA. The Common Mistakes section describes common problems that people have when doing the things in this tutorial, and how to resolve them. The Frequently Asked Questions section describes questions that people have that aren't related to mistakes, but rather, trying to understand the stuff better or exploring how it can be used. If your problem or concern isn't addressed here, feel free to add a comment below, so that I know where you're having trouble. I like to keep these pages fairly clean, so I may remove comments that I felt like have been addressed. If I remove your comment and you don't feel like the problem has been fixed, repost the question and we'll take another look at it. If a tutorial has a mistake in it, I will fix the mistake and reply to the comment with a brief explanation. However, after a couple of weeks I'll likely go back and remove the original comment as well as my reply, because, hopefully, the problem will have been fixed, and it won't be a concern any more. |
Common MistakesNone listed yet… |
Frequently Asked QuestionsNone listed yet… |
Hello Mr. Whitaker,
I found a problem in "Changing The Window Size"
Using the GraphicsDevice before LoadContent produces an error. I suggest you mention that it should be called after LoadContent (at end of the LoadContent method also works).
Thanks for your great tutorials!
"NullReferenceException was unhandled" when I run the program…
Running on XNA 4.0.
I apologize for getting back to this comment only after a very long time. Hopefully you actually made it through this a long time ago, and my comment below can be helpful to others in the future.
So the code for changing window size can go almost anywhere in your game, but not quite everywhere. It requires the graphics object already be initialized, and so if you put this code at the top of your constructor, before the line graphics = new GraphicsDeviceManager(this);, you'll get a NullReferenceException because… well… graphics is still null. It doesn't have a value yet. So you have to put the code in the tutorials after this line and you should avoid the exception.
Andrew's example, below, illustrates this correctly.
Thank-you,
As with all coding, I find, the devil is in the detail.
And he'll bite you on the a** if he gets half a chance.
Cheers,
A.
So true, Andrew. Which is why it's important to learn the fundamental concepts instead of copying and pasting code from the Internet. I learned that the hard way back in my early days…
I too kept getting the NullReferenceException error.
This is the correct code and the correct place to put it (IMHO).
public Game1()
{
Content.RootDirectory = "Content";
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferHeight = 800;
graphics.PreferredBackBufferWidth = 1200;
}
Best of luck,
Andrew.
I'm having an issue when trying to resize a window when combined with your MonoGame 2D background tutorial (I can't post links but OKE6T9Y.png on imgur.com)
Basically the full image is displayed when at 800x480 yet when I swap to 1280x768 it gets cropped. The area in red is what's being displayed in the larger window and for some reason it starts drawing at 0,288 (768-480). I've tried creating a basic standalone app just to test this and it still keeps on happening. Code is as follows:
Any ideas where I'm going wrong with this?
I know this is several years old, but for the record, I have just tested your code (only switching font and assets’s names to my own) and it worked fine! The resized window never showed blue, whether I had even toggled full screen mode or not.
I guess MonoGame has improved since then; in particular, the full screen toggling issue mentioned in tutorial 3 has been solved according to the given reference*, so this may be just one of those former problems.
* (remove spaces)
github . com/MonoGame/MonoGame/issues/2495
I just want to report one small problem(?): I reused the 2D tutorials’ code for this one; in particular, clicking has particles pop around my mouse pointer.
I realize that when I toggle the resolution from default to those of my screen (using PreferredBackBufferWidth and PreferredBackBufferHeight) with or without full screen mode, the particles then get generated slightly above the mouse pointer (or below in full screen mode); in fact, this may be linked to the window’s location a bit below the real top of my screen.
Once I move the window a bit, the particles get generated at the right location once again. So I suppose the resolution switch can lead to a ‘decoherence’ (appropriate for particles, if you are into quantum physics! ;)) between the program’s internal coordinates and those displayed, if said switch causes the window to get moved around.
I have just tried using Mouse.SetPosition to force the mouse pointer into some specific coordinates, but this does not remove the shift.
Although I have not tried the following, I guess using an image as a proxy for the mouse pointer should solve the problem, because it would then probably be located at the program’s internal coordinates.
Post preview:
Close preview