My code is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace King_platformer
{
class KingCharacter : Character
{
public Texture2D Texture { get; set; }
public Vector2 Location;
private Vector2 Movement;
private Vector2 Gravity;
private const int width = 16;
private const int height = 22;
private bool flipped;
private int currentFrame;
private int endFrame;
private int startFrame;
private int count = 0;
private int timeSinceLastFrame = 0;
private bool hasJumped;
private const int millisecondsPerFrame = 250;
public KingCharacter(Texture2D texture)
{
// constructor
Location = new Vector2(100, 100);
Movement = new Vector2(0, 0);
Texture = texture;
currentFrame = 0;
flipped = false;
hasJumped = false;
}
struct Frames
{
}
public override static Animation(ref int currentFrame, int endFrame, int startFrame, ref int timeSinceLastFrame,GameTime gameTime,int millisecondsPerFrame,ref int count)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame && count > 0)
{
timeSinceLastFrame -= gameTime.ElapsedGameTime.Milliseconds;
count--;
return currentFrame--;
}
else if (endFrame == 0)
{
count = 0;
timeSinceLastFrame -= gameTime.ElapsedGameTime.Milliseconds;
return currentFrame = startFrame;
}
else if (timeSinceLastFrame > millisecondsPerFrame && currentFrame == endFrame)
{
timeSinceLastFrame -= millisecondsPerFrame;
count = endFrame - 1;
return currentFrame--;
}
else if (timeSinceLastFrame > millisecondsPerFrame && currentFrame < endFrame)
{
timeSinceLastFrame -= millisecondsPerFrame;
return currentFrame++;
}
return currentFrame;
}
public void Update(GameTime gameTime)
{
KeyboardState ks = Keyboard.GetState();
if (ks.IsKeyDown(Keys.A))
{
flipped = true;
Movement.X =-1;
Movement.Y =+ 0;
endFrame = 3;
startFrame = 0;
Animation(ref currentFrame, endFrame, startFrame, ref timeSinceLastFrame, gameTime, millisecondsPerFrame, ref count);
}
if (ks.IsKeyDown(Keys.D))
{
flipped = false;
Movement.X =+ 1;
Movement.Y =+ 0;
endFrame = 3;
startFrame = 0;
Animation(ref currentFrame, endFrame, startFrame, ref timeSinceLastFrame, gameTime, millisecondsPerFrame, ref count);
}
if (ks.IsKeyDown(Keys.W) && hasJumped == false)
{
Movement.Y -= 10f;
Gravity.Y -= 5f;
//endFrame
hasJumped = true;
}
if (ks.GetPressedKeys().Length == 0)
{
Movement.X = +0f;
Movement.Y =+ 0f;
endFrame = 0;
startFrame = 0;
Animation(ref currentFrame, endFrame, startFrame, ref timeSinceLastFrame, gameTime, millisecondsPerFrame, ref count);
}
if (hasJumped == true)
{
float i = 1;
Gravity.Y += 0.15f * i;
}
Location += Movement + Gravity;
}
public void Draw(SpriteBatch spriteBatch, Vector2 Location)
{
//int width = Texture.Width / 23;
//int height = Texture.Height / 4;
//var effects = SpriteEffects.None;
//Rectangle sourceRectangle = new Rectangle(currentFrame * width, height * 1, width, height);
//Rectangle destinationRectangle = new Rectangle((int)Location.X, (int)Location.Y, width * 2, height * 2);
int width = 354 / 23;
int height = Texture.Height / 4;
var effects = SpriteEffects.None;
Rectangle sourceRectangle = new Rectangle(currentFrame * width, height, width, height);
Rectangle destinationRectangle = new Rectangle((int)Location.X, (int)Location.Y, width * 2, height * 2);
if (flipped == true)
{
effects = SpriteEffects.FlipHorizontally;
}
else if (flipped == false)
{
effects = SpriteEffects.None;
}
spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White, 0f, Vector2.Zero, effects, 1f);
}
}
}
I need implement a “Person” class so I can use the methods in the other classes.