LetsRaceBwoi
Well-Known Member
If someone could tell me why this code isn't working, please do!
Screen.cs:
CharacterCreation.cs:
GameController.cs:
Game1.cs:
As you can see I'm asking it to draw, but it never calls Update for some reason even though it's called in Game1.cs. I'm confused as to how it's not working and it's such simple code, too.
(taken from https://github.com/LRB-Game-Tools/Platformer2)
Screen.cs:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Platformer2
{
class Screen
{
public Screen()
{
}
public void Init(GraphicsDevice _device, SpriteBatch _spriteBatch)
{
}
public void Update()
{
}
}
}
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Platformer2
{
class CharacterCreation : Screen
{
GraphicsDevice device;
SpriteBatch spriteBatch;
public CharacterCreation()
{
}
public void Init(GraphicsDevice _device, SpriteBatch _spriteBatch)
{
device = _device;
spriteBatch = _spriteBatch;
}
public void Update()
{
Texture2D blankTex = new Texture2D(device, 1, 1);
Color[] cl = {Color.FromNonPremultiplied(255, 100, 100, 100)};
blankTex.SetData<Color>(cl);
spriteBatch.Draw(blankTex, new Rectangle(0, 0, 1920, 1080), Color.White);
}
}
}
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Platformer2
{
class GameController
{
public Dictionary<string, Screen> Screens = new Dictionary<string, Screen>() {
{ "MENU", new Menu() },
{ "CHARACTERCREATION", new CharacterCreation() },
{ "ERROR", new Error() }
};
public string Screen; // case insensitive
# if DEBUG
private bool CheatsEnabled = true;
# else
private bool CheatsEnabled = false;
# endif
public GameController(GraphicsDevice _device, SpriteBatch _spriteBatch, String _Screen = null)
{
Screen = _Screen ?? "MENU";
this.Init(_device, _spriteBatch);
}
public void Init(GraphicsDevice _device, SpriteBatch _spriteBatch)
{
Screens[Screen ?? "ERROR"].Init(_device, _spriteBatch);
}
public void Update()
{
Screens[Screen ?? "ERROR"].Update();
}
}
}
Code:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Platformer2
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
GameController gameController;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
IsMouseVisible = true;
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
gameController = new GameController(graphics.GraphicsDevice, spriteBatch);
gameController.Screen = "CHARACTERCREATION";
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
gameController.Update();
spriteBatch.End();
base.Draw(gameTime);
}
}
}
(taken from https://github.com/LRB-Game-Tools/Platformer2)