سؤال

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