Position initiale du personnage
Bonjour,
j'ai commencé un petit jeu, mais je bloque quant à la position du personnage en début de partie, je sais pas quoi rajouter ni où pour pouvoir le positionner :s
Les 2 classes concernées :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
namespace test
{
public class Player
{
Texture2D playerImage;
Vector2 playerPosition;
Vector2 tempCurrentFrame;
KeyboardState keyState;
float moveSpeed = 300f;
Animation playerAnimation = new Animation();
public void Initialize()
{
playerAnimation.Initialize(playerPosition, new Vector2(6, 2));
tempCurrentFrame = Vector2.Zero;
}
public void LoadContent(ContentManager Content)
{
playerImage = Content.Load<Texture2D>("mainsheet");
playerAnimation.AnimationImage = playerImage;
}
public void Update(GameTime gameTime)
{
keyState = Keyboard.GetState();
playerAnimation.Active = true;
playerPosition = playerAnimation.Position;
if (keyState.IsKeyDown(Keys.Right))
{
playerPosition.X += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
tempCurrentFrame.Y = 0;
}
else if (keyState.IsKeyDown(Keys.Left))
{
playerPosition.X -= moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
tempCurrentFrame.Y = 1;
}
else playerAnimation.Active = false;
tempCurrentFrame.X = playerAnimation.CurrentFrame.X;
playerAnimation.Position = playerPosition;
playerAnimation.CurrentFrame = tempCurrentFrame;
playerAnimation.Update(gameTime);
}
public void Draw(SpriteBatch spriteBatch)
{
playerAnimation.Draw(spriteBatch);
}
}
} |
et
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
namespace test
{
public class Animation
{
int nbFrame;
int switchFrame;
bool active;
Vector2 position;
Vector2 frameAmount;
Vector2 currentFrame;
public Texture2D Image;
Rectangle sourceRect;
public bool Active
{
get { return active; }
set { active = value; }
}
public Vector2 CurrentFrame
{
get { return currentFrame; }
set { currentFrame = value; }
}
public Vector2 Position
{
get { return position; }
set { position = value; }
}
public Texture2D AnimationImage
{
set { Image = value; }
}
public int FrameWidth
{
get { return Image.Width / (int)frameAmount.X; }
}
public int FrameHeight
{
get { return Image.Height / (int)frameAmount.Y; }
}
public void Initialize(Vector2 position, Vector2 Frames)
{
active = false;
switchFrame = 120;
this.position = position;
this.frameAmount = Frames;
}
public void Update(GameTime gameTime)
{
if (active)
{
nbFrame += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
}
else nbFrame = 0;
if (nbFrame >= switchFrame)
{
nbFrame = 0;
currentFrame.X += FrameWidth;
if (currentFrame.X >= Image.Width)
{
currentFrame.X = 0;
}
}
sourceRect = new Rectangle((int)currentFrame.X, (int)currentFrame.Y * FrameHeight, FrameHeight, FrameWidth);
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Image, position, sourceRect, Color.White);
}
}
} |
merci d'avance