Here are the 3 main files used in this problem:
//Entity.h
#include "Engine.h"
namespace DarkMatter
{
class Entity
{
protected:
LPDIRECT3DTEXTURE9 texture;
D3DXVECTOR3 pos;
int health;
int frame;
int starttime;
int width;
int height;
public:
Entity();
~Entity();
void LoadTexture(string filename, D3DCOLOR trans);
void DrawFrame(int width, int height, int columns);
void Animate(int startframe, int endframe, int direction, int delay);
void SetPos(float x, float y);
void SetDimensions(string filename);
virtual void Move() = 0;
virtual void Update() = 0;
};
class Player : public Entity
{
private:
int health;
int frame;
int starttime;
int width;
int height;
enum Movement {standing, running, jumping, crouching};
Movement state;
public:
Player();
~Player();
void Move();
void Update();
};
};//Entity.cpp
#include "Entity.h"
namespace DarkMatter
{
Entity::Entity()
{
this -> texture = NULL;
this -> pos.x = 0; this -> pos.y = 0;
this -> health = 0;
this -> frame = 0;
this -> starttime = 0;
this -> width = 0;
this -> height = 0;
}
Entity::~Entity()
{
}
void Entity::LoadTexture(string filename, D3DCOLOR trans)
{
D3DXIMAGE_INFO info;
D3DXGetImageInfoFromFile(filename.c_str(), &info);
D3DXCreateTextureFromFileEx(
engine -> GetDevice(),
filename.c_str(),
info.Width, info.Height,
1,
D3DPOOL_DEFAULT,
D3DFMT_UNKNOWN,
D3DPOOL_DEFAULT,
D3DX_DEFAULT,
D3DX_DEFAULT,
trans,
&info,
NULL,
&this -> texture);
}
void Entity::DrawFrame(int width, int height, int columns)
{
D3DXVECTOR3 pos(this -> pos.x, this -> pos.y, 0);
D3DCOLOR white = D3DCOLOR_XRGB(255, 255, 255);
RECT rect;
rect.left = (this -> frame % columns) * width;
rect.top = (this -> frame / columns) * height;
rect.right = rect.left + width;
rect.bottom = rect.top + height;
engine -> GetSprite() -> Draw(this -> texture, &rect, NULL, &pos, white);
}
void Entity::Animate(int startframe, int endframe, int direction, int delay)
{
if ((int)GetTickCount() > this -> starttime + delay)
{
this -> starttime = GetTickCount();
this -> frame += direction;
if (this -> frame > endframe) this -> frame = startframe;
if (this -> frame < startframe) this -> frame = endframe;
}
}
void Entity::SetPos(float x, float y)
{
this -> pos.x = x;
this -> pos.y = y;
}
void Entity::SetDimensions(string filename)
{
D3DXIMAGE_INFO info;
D3DXGetImageInfoFromFile(filename.c_str(), &info);
this -> width = info.Width;
this -> height = info.Height;
}};//Player.cpp
#include "Entity.h"
namespace DarkMatter
{
Player::Player()
{
this -> health = 0;
this -> frame = 0;
this -> starttime = 0;
this -> width = 37;
this -> height = 44;
}
Player::~Player()
{
}
void Player::Move()
{
engine -> GetKeyboard() -> Acquire();
engine -> GetKeyboard() -> GetDeviceState(sizeof(engine -> keys), (LPVOID)&engine -> keys);
if (engine -> keys[DIK_LEFT] & 0x80)
{
this -> pos.x -= 3.0f;
this -> state = running;
}
else if (engine -> keys[DIK_RIGHT] & 0x80)
{
this -> pos.x += 3.0f;
this -> state = running;
}
else
{
this -> state = standing;
this -> frame = 0;
}
if (engine -> keys[DIK_UP] & 0x80)
{
this -> pos.y -= 3.0f;
this -> state = standing;
}
else if (engine -> keys[DIK_DOWN] & 0x80)
{
this -> pos.y += 3.0f;
this -> state = standing;
}
else
{
this -> state = standing;
this -> frame = 0;
}
if (engine -> keys[DIK_ESCAPE] & 0x80)
{
gameover = true;
}
}
void Player::Update()
{
this -> Move();
switch (this -> state)
{
case running:
{
this -> DrawFrame(37, 44, 6);
this -> Animate(0, 5, 1, 60);
break;
}
case standing:
{
this -> DrawFrame(37, 44, 6);
break;
}
case jumping:
{
break;
}
case crouching:
{
break;
}
default:
{
}
}
}};As you can see i made an Enum in the Player class that holds all the possible movement types. Then when the player presses the keys to make the sprite move the state changes from standing to running. In the Update() function it uses a switch statement to check which state the character is in to make the corrisponding animation. However it jumps straight to "case running" and makes a continuous running animation. If you could help me out then thanks






