I am creating a scaling matrix for my 2d tower defense game with this code:

float screenscalex;
float screenscaley;
Matrix SpriteScale;
screenscalex = (float)_graphics.GraphicsDevice.Viewport.Width / 1366f;
screenscaley = (float)_graphics.GraphicsDevice.Viewport.Height / 768f;
SpriteScale = Matrix.CreateScale(screenscalex, screenscaley, 1);

However it says that where screenscalex is initialized that it is a null reference. Does anyone know what I'm doing wrong? I tried using GraphicsAdapter to get the width of the screen but it didn't work. Also, I haven't set the viewport to anything. Finally, moving the code to Initialize() just makes the game crash

有帮助吗?

解决方案

You have to do this below base.Initialize() which is where that is initialized at..

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top