Nothing appears on screen when running

Started by
2 comments, last by raidzero 12 years, 1 month ago
I go to debug my program but nothing appears on my screen. I open task manager and I can see the process though.

Main.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using TestGame.Controls;
using TestGame.GameStates;
namespace TestGame
{
public class Main : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
InputHandler inputHandler;
public SpriteBatch spriteBatch;
SpriteFont spriteFont;
MainMenu mainMenu;
Vector2 position;
Texture2D backgroundImage;
public Main()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
inputHandler = new InputHandler();
mainMenu = new MainMenu();
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
}

protected override void Initialize()
{
this.IsMouseVisible = true;

base.Initialize();
mainMenu.MenuInitialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
spriteFont = Content.Load<SpriteFont>(@"Fonts\MainFont");
backgroundImage = Content.Load<Texture2D>(@"Backgrounds\titlescreen");
mainMenu.MenuLoadContent();
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
inputHandler.Update();
if (inputHandler.currentKeyState.IsKeyDown(Keys.Escape))
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
spriteBatch.Draw(backgroundImage, new Vector2(0, 0), Color.White);
mainMenu.MenuDraw();
spriteBatch.End();
base.Draw(gameTime);
}
}
}


MainMenu.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace TestGame.GameStates
{
public class MainMenu : Main
{
enum buttonState { hover, up, released, down }
const int numberOfButtons = 4, newGameButtonIndex = 0, loadGameButtonIndex = 1, optionsButtonIndex = 2, quitButtonIndex = 3, buttonHeight = 48, buttonWidth = 80;
Color backgroundButtonColor;
Color[] buttonColor = new Color[numberOfButtons];
Rectangle[] buttonRect = new Rectangle[numberOfButtons];
buttonState[] buttonSt = new buttonState[numberOfButtons];
Texture2D[] buttonTexture = new Texture2D[numberOfButtons];
double[] buttonTimer = new double[numberOfButtons];
bool mousePressed, previouslyPressed = false;
int mouseX, mouseY;
double frameTime;
int buttonPadding;
public void MenuInitialize()
{
for (int i = 0; i < numberOfButtons; i++)
{
buttonSt = buttonState.up;
buttonColor = Color.White;
buttonTimer = 0.0;
buttonRect = new Rectangle(0, buttonPadding, buttonWidth, buttonHeight);
buttonPadding += buttonHeight;
}
}
public void MenuLoadContent()
{
buttonTexture[newGameButtonIndex] = Content.Load<Texture2D>(@"Sprites\desktop");
buttonTexture[loadGameButtonIndex] = Content.Load<Texture2D>(@"Sprites\desktop");
buttonTexture[optionsButtonIndex] = Content.Load<Texture2D>(@"Sprites\desktop");
buttonTexture[quitButtonIndex] = Content.Load<Texture2D>(@"Sprites\desktop");
}
public void MenuDraw()
{
for (int i = 0; i < numberOfButtons; i++)
{
spriteBatch.Draw(buttonTexture, buttonRect, buttonColor);
}
}
}
}
Advertisement
I managed to get it to run, but I don't understand why it worked.

Main.cs

public class Main : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
InputHandler inputHandler;
public SpriteBatch spriteBatch;
SpriteFont spriteFont;
MainMenu mainMenu;
Vector2 position;
Texture2D backgroundImage;
public Main()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
inputHandler = new InputHandler();
mainMenu = new MainMenu(this);
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
}


MainMenu.cs

public class MainMenu
{
Main main;
public MainMenu(Game game)
{
main = (Main)game;
}


The bold is what I changed. Could anyone explained to me what I did?
I have the same problem, and I tried your suggested change too but it did not fix my problem at all! I am feeling stuck! Any other tips please? sad.png





This is an excellent site I love it

I have the same problem, and I tried your suggested change too but it did not fix my problem at all! I am feeling stuck! Any other tips please? sad.png


I think the reason for my problem is that I was creating a new instance of each object in the constructors. So when Main read mainMenu = new MainMenu(); it would then read MainMenu's constructor which was main = new Main(); which would then go back and read mainMenu = new MainMenu(); creating a loop.

This topic is closed to new replies.

Advertisement