I'm kind of new to XNA, and I watched some tutorials but for some reason my sprite won't move around when I press the keys. It just sits there.
[code]
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 Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace WindowsGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Vector2 position = Vector2.Zero;
Vector2 velocity;
Rectangle world_size;
Texture2D charactor;
Texture2D World;
KeyboardState key = new KeyboardState();
Vector2 size = new Vector2(1024, 0);
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
world_size = new Rectangle(0, 500, 1024, 100);
velocity = new Vector2(1, 0);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
charactor = this.Content.Load<Texture2D>("Sprites/figure-1");
World = this.Content.Load<Texture2D>("world");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (key.IsKeyDown(Keys.Right))
{
position.X += velocity.X;
}
else if(key.IsKeyDown(Keys.Left))
{
position.X -= velocity.X;
}
else
{
position.X = position.X;
}
base.Update(gameTime);
}
public void movement(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(World, world_size, Color.White);
spriteBatch.Draw(charactor, position, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
[/code]
What am I doing wrong?
You need to get the KeyboardState from XNA instead of thin air.
[QUOTE=Ortzinator;25475274]You need to get the KeyboardState from XNA instead of thin air.[/QUOTE]
The keyboard state is in the first lines of the files.
[code]
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Vector2 position = Vector2.Zero;
Vector2 velocity;
Rectangle world_size;
Texture2D charactor;
Texture2D World;
[B] KeyboardState key = new KeyboardState(); [/B]
Vector2 size = new Vector2(1024, 0);
[/code]
Sorry i cant help you though i noticed character is spelled wrong.
[QUOTE=CammieDee666;25475313]Sorry i cant help you though i noticed character is spelled wrong.[/QUOTE]
True, but It really doesn't matter how I spell it.
[QUOTE=wilderbeast;25475298]The keyboard state is in the first lines of the files.
[code]
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Vector2 position = Vector2.Zero;
Vector2 velocity;
Rectangle world_size;
Texture2D charactor;
Texture2D World;
[B] KeyboardState key = new KeyboardState(); [/B]
Vector2 size = new Vector2(1024, 0);
[/code][/QUOTE]
Exactly, you're making it out of thin air. Use Keyboard.GetState.
[code]
else
{
position.X = position.X;
}
[/code]
What is the point of this?
[QUOTE=Xeon06;25475404]Exactly, you're making it out of thin air. Use Keyboard.GetState.[/QUOTE]
My bad, I thought you meant the entire thing didn't exist.
Still doesn't work.
[editline]18th October 2010[/editline]
[QUOTE=SamPerson123;25475452][code]
else
{
position.X = position.X;
}
[/code]
What is the point of this?[/QUOTE]
I don't really know if it stopped it, because I used to use a language when if you didn't put an else, it wouldn't stop the object from moving.
[QUOTE=wilderbeast;25475354]True, but It really doesn't matter how I spell it.[/QUOTE]
Maybe when its just you using it, but if anyone else uses the code and they access a misspelled variable they could spell it correctly which would lead to errors.
Post your new code when you're actually getting the key.
[QUOTE=geel9;25475663]Post your new code when you're actually getting the key.[/QUOTE]
[code]
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 Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace WindowsGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Vector2 position = Vector2.Zero;
Vector2 velocity;
Rectangle world_size;
Texture2D charactor;
Texture2D World;
KeyboardState key = Keyboard.GetState();
Vector2 size = new Vector2(1024, 0);
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
world_size = new Rectangle(0, 500, 1024, 100);
velocity = new Vector2(1, 1);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
charactor = this.Content.Load<Texture2D>("Sprites/figure-1");
World = this.Content.Load<Texture2D>("world");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (key.IsKeyDown(Keys.Right))
{
position.X += velocity.X;
}
else if(key.IsKeyDown(Keys.Left))
{
position.X -= velocity.X;
}
base.Update(gameTime);
}
public void movement(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(World, world_size, Color.White);
spriteBatch.Draw(charactor, position, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
[/code]
Move
KeyboardState key = Keyboard.GetState();
to the update function
edit to explain:
At the moment your code simply gets the keyboard state when the program is first launched meaning that if you held down right when you launched it, your player would probably keep on moving right all the time.
You need to put it in the update function to make it keep checking for the keyboard state
[QUOTE=Richy19;25475821]Move
KeyboardState key = Keyboard.GetState();
to the update function[/QUOTE]
Ah, well that explains it.
This entire section of code:
[code]
public void movement(GameTime gameTime)
{
base.Update(gameTime);
}
[/code]
is there for no reason at all, it never gets called and if it is all it does it trigger base.update witch is already done in the update method
You shouldn't jump straight into game programming without learning C++ more profusely (correct word?) first. I made that mistake, and struggled a lot, making many programming errors and almost gave up. I then decided to go to absolute basics, buy a book and begin learning again.
I not have much more interest in it, and am still doing command-line application after months. It shouldn't just take a few days before you go ahead and try game programming.
[QUOTE=nos217;25483147]You shouldn't jump straight into game programming without learning C++ more profusely (correct word?) first. I made that mistake, and struggled a lot, making many programming errors and almost gave up. I then decided to go to absolute basics, buy a book and begin learning again.
I not have much more interest in it, and am still doing command-line application after months. It shouldn't just take a few days before you go ahead and try game programming.[/QUOTE]
This is C#, although i get your point and i'm sure it was only a spelling mistake.
Just saying. :3:
I was bored so I cleaned up your code and got it to work, hopefully you understand what I have changed and why but really you should learn object orientated programming and read an XNA book.
[code]
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 Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace WindowsGame1
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Vector2 playerPosition = Vector2.Zero;
Vector2 playerVelocity = Vector2.One;
Texture2D playerTexture;
Texture2D backgroundTexture;
KeyboardState keyState;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
playerTexture = this.Content.Load<Texture2D>("Sprites/figure-1");
backgroundTexture = this.Content.Load<Texture2D>("world");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Right))
playerPosition.X += playerVelocity.X;
if (keyState.IsKeyDown(Keys.Left))
playerPosition.X -= playerVelocity.X;
if (keyState.IsKeyDown(Keys.Up))
playerPosition.Y -= playerVelocity.Y;
if (keyState.IsKeyDown(Keys.Down))
playerPosition.Y += playerVelocity.Y;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(backgroundTexture, graphics.GraphicsDevice.Viewport.Bounds, Color.White);
spriteBatch.Draw(playerTexture, playerPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
[/code]
Key needs to be updated as Keyboardstate each frame.
In Update() add key = Keyboard.GetState();.
hmm... fairly ninja'd
[QUOTE=CrumbleShake;25486613]Key needs to be updated as Keyboardstate each frame.
In Update() add key = Keyboard.GetState();.
hmm... fairly ninja'd[/QUOTE]
don't worry only by 15 hours
[code]
if(Keboard.GetState().IsKeyDown(Keys.W))
Position.Y --; //Move UP
[/code]
You could later add support for checking single key presses like this:
[code]
KeyboardState game_kb, game_kb_old;
void updateKeyboard()
{
if(keyOnce(Keys.Space))
fireBullet();
//check other keys here
game_kb_old = game_kb;
}
public bool keyOnce(Keys k)
{
if (game_kb.IsKeyDown(k) && !game_kb_old.IsKeyDown(k))
return true;
return false;
}
[/code]
Sorry, you need to Log In to post a reply to this thread.