I'm making a new game (in Visual Studio, using MonoGame template), and I figured that classes make everything tidier and easier to find/work with - my one problem is that I have no idea how to link one class to another.
Lemme go more into depth - I'm just working with moving the player in the direction corresponding to WASD keys. I've got code that works fine when in the normal Game1 class, but I want it to be in a class specifically for keyboard input.
Note - I haven't touched any of the "using" stuff at the top of the classes.
here's my code:
Keyboard Input class:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace rpg_game
{
internal class KeyboardInput
{
public int player_X;
public int player_Y;
KeyboardState keyboardState = Keyboard.GetState();
public void Update(GameTime gameTime)
{
if (keyboardState.IsKeyDown(Keys.W)) { player_Y -=1; }
if (keyboardState.IsKeyDown(Keys.A)) { player_X -= 1; }
if (keyboardState.IsKeyDown(Keys.S)) { player_Y += 1; }
if (keyboardState.IsKeyDown(Keys.D)) { player_X += 1; }
}
}
}
just to clarify, if theres /* ... */ within the classes, it just means that I've not added or taken anything from them - those classes are untouched :)
Game1 class:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace rpg_game
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private Texture2D player;
private int player_X = 100;
private int player_Y = 38;
KeyboardInput input = new KeyboardInput();
public Game1() { /* ... */ }
protected override void Initialize() { /* ... */ }
protected override void LoadContent()
{
player = Content.Load<Texture2D>("player");
}
protected override void Update(GameTime gameTime)
{
input.player_X = player_X;
input.player_Y = player_Y;
input.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin();
_spriteBatch.Draw(player, new Rectangle(player_X, player_Y, 25, 25), Color.White);
_spriteBatch.End();
base.Draw(gameTime);
}
}
}
I've provided pretty much everything in each class, but in the Game1 class, the problem (I think), is in the Update.
My question, in short, is how can I get it so that the keyboard input is in another class, yet works the same way as when it was all in the Game1 class?
With it now in a different place, the player doesn't move when I run the code. What do I need to do in order to use classes and still get the code to do what it's supposed to do??
In your keyboard class, you have defined a new variable of
player_Xandplayer_Y.Those are different from the
player_Xandplayer_Yyou used inGame1, so it doesn't make a connection between them.Adding a parameter to the
KeyboardInput.Update()could work, but those will only update if you use ref, like this:Additionally, you can also update variables if you bring a class as a parameter, you could still use
refas an easy way out, but I think that'll be messy with manyrefparameters.I think you're on the right path to use classes, but I'll also recommend to make a
Playerclass as well. and place theplayer_Xandplayer_Ythere. From there, you either add thePlayerclass to the parameter ofKeyboardInput.Update(), or you could place theUpdate()code in the Player class instead. (the latter would be my preference)Example of a Player class:
Then, in the Game1.cs, you could define the Player class there:
(You'll need to remove the X and Y variables in the Game1 class, as those will no longer be relevant)
If you still wish to use the KeyboardInput.Update instead of the Player.Update, then you could remove the
Player.Update()method, and add the parameter to KeyboardInput.Update (Theplayer_x/player_Yneeds to be changed as well):