REDNIK
Legacy Member
Yo, effe het volgende probleem. Ik wil een ventje laten bewegen over mijn beeld, ik heb hiervoor een sprite bestand waar dat het personage naar boven wandelt, naar onder, enzovoort.
Ik kan het personage laten bewegen over heel het veld, maar nu gaat hij heel de tijd de lus af en draait het personage naar alle kanten. Maar het is dus de bedoeling dat wanneer ik naar rechts druk de personage naar rechts kijkt, enzovoort. Ik wil niet met rotate werken, ik heb voor al de 4 richtingen aparte sprites.
Ik kan het personage laten bewegen over heel het veld, maar nu gaat hij heel de tijd de lus af en draait het personage naar alle kanten. Maar het is dus de bedoeling dat wanneer ik naar rechts druk de personage naar rechts kijkt, enzovoort. Ik wil niet met rotate werken, ik heb voor al de 4 richtingen aparte sprites.
Code:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
namespace Bobberman
{
/// <summary>
/// This is a game component that implements IUpdateable.
/// </summary>
public class Bobberman : Microsoft.Xna.Framework.DrawableGameComponent
{
protected Texture2D texture;
protected Vector2 position = new Vector2();
protected const double WIDTH = 29.27;
protected const double HEIGHT = 28.17;
protected Rectangle screenBounds;
protected List<Rectangle> framesRight;
protected List<Rectangle> framesLeft;
protected List<Rectangle> framesUp;
protected List<Rectangle> framesDown;
protected List<Rectangle> frames;
private int activeFrame;
SpriteBatch sBatch;
int lastFrameTime;
int i;
const int TIME_BETWEEN_FRAMES = 300;
public Bobberman(Game game, Texture2D texture)
: base(game)
{
this.texture = texture;
screenBounds = new Rectangle(0, 0,
Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height);
frames = new List<Rectangle>();
framesRight = new List<Rectangle>();
framesLeft = new List<Rectangle>();
framesUp = new List<Rectangle>();
framesDown = new List<Rectangle>();
lastFrameTime = System.Environment.TickCount;
CreateFrames();
activeFrame = 0;
PutInStartPosition();
}
public void PutInStartPosition()
{
position = new Vector2(20, 20);
}
public void CreateFrames()
{
//right
frames.Add(new Rectangle(3 * (int)WIDTH, (int)HEIGHT, (int)WIDTH, (int)HEIGHT));
frames.Add(new Rectangle(3 * (int)WIDTH, 2 * (int)HEIGHT, (int)WIDTH, (int)HEIGHT));
//left
frames.Add(new Rectangle(1 * (int)WIDTH, (int)HEIGHT, (int)WIDTH, (int)HEIGHT));
frames.Add(new Rectangle(1 * (int)WIDTH, 2 * (int)HEIGHT, (int)WIDTH, (int)HEIGHT));
//up
frames.Add(new Rectangle(2 * (int)WIDTH, (int)HEIGHT, (int)WIDTH, (int)HEIGHT));
frames.Add(new Rectangle(2 * (int)WIDTH, 2 * (int)HEIGHT, (int)WIDTH, (int)HEIGHT));
//down
frames.Add(new Rectangle(0 * (int)WIDTH, (int)HEIGHT, (int)WIDTH, (int)HEIGHT));
frames.Add(new Rectangle(0 * (int)WIDTH, 2 * (int)HEIGHT, (int)WIDTH, (int)HEIGHT));
}
/// <summary>
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
/// </summary>
public override void Initialize()
{
// TODO: Add your initialization code here
base.Initialize();
}
/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
Boolean spriteMoves = false;
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Right))
{
position.X += 2.5f;
spriteMoves = true;
}
if (keyboardState.IsKeyDown(Keys.Left))
{
position.X -= 2.5f;
spriteMoves = true;
}
if (keyboardState.IsKeyDown(Keys.Up))
{
position.Y -= 2.5f;
spriteMoves = true;
}
if (keyboardState.IsKeyDown(Keys.Down))
{
position.Y += 2.5f;
spriteMoves = true;
}
if (spriteMoves)
{
if (System.Environment.TickCount - lastFrameTime >
TIME_BETWEEN_FRAMES)
{
activeFrame = (activeFrame + 1) % frames.Count;
lastFrameTime = System.Environment.TickCount;
}
}
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
SpriteBatch sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
sBatch.Draw(texture, position, frames[activeFrame], Color.White);
base.Draw(gameTime);
}
}
}