XNA 2D Board game - trouble with the cursor
- by Adorjan
I just have started making a simple 2D board game using XNA, but I got stuck at the movement of the cursor.
This is my problem:
I have a 10x10 table on with I should use a cursor to navigate. I simply made that table with the spriteBatch.Draw() function because I couldn't do it on another way.
So here is what I did with the cursor:
public override void LoadContent()
{
...
mutato.Position = new Vector2(X, Y); //X=103, Y=107;
mutato.Sebesseg = 45;
...
mutato.Initialize(content.Load<Texture2D>("cursor"),mutato.Position,mutato.Sebesseg);
...
}
public override void HandleInput(InputState input)
{
if (input == null)
throw new ArgumentNullException("input");
// Look up inputs for the active player profile.
int playerIndex = (int)ControllingPlayer.Value;
KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
{
ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
}
else
{
// Otherwise move the player position.
if (keyboardState.IsKeyDown(Keys.Down))
{
Y = (int)mutato.Position.Y + mutato.Move;
}
if (keyboardState.IsKeyDown(Keys.Up))
{
Y = (int)mutato.Position.Y - mutato.Move;
}
if (keyboardState.IsKeyDown(Keys.Left))
{
X = (int)mutato.Position.X - mutato.Move;
}
if (keyboardState.IsKeyDown(Keys.Right))
{
X = (int)mutato.Position.X + mutato.Move;
}
}
}
public override void Draw(GameTime gameTime)
{
mutato.Draw(spriteBatch);
}
Here's the cursor's (mutato) class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Battleship.Components
{
class Cursor
{
public Texture2D Cursortexture;
public Vector2 Position;
public int Move;
public void Initialize(Texture2D texture, Vector2 position,int move)
{
Cursortexture = texture;
Position = position;
Move = move;
}
public void Update()
{
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Cursortexture, Position, Color.White);
}
}
}
And here is a part of the InputState class where I think I should change something:
public bool IsNewKeyPress(Keys key, PlayerIndex? controllingPlayer,
out PlayerIndex playerIndex)
{
if (controllingPlayer.HasValue)
{
// Read input from the specified player.
playerIndex = controllingPlayer.Value;
int i = (int)playerIndex;
return (CurrentKeyboardStates[i].IsKeyDown(key) &&
LastKeyboardStates[i].IsKeyUp(key));
}
}
If I leave the movement operation like this it doesn't have any sense:
X = (int)mutato.Position.X - mutato.Move;
However if I modify it to this:
X = (int)mutato.Position.X--;
it moves smoothly. Instead of this I need to move the cursor by fields (45 pixels), but I don't have any idea how to manage it.