My character.h
looks as follows:
#include "raylib.h"
class Character
{
public:
Character(int winWidth, int winHeight);
Vector2 getWorldPos() { return worldPos; }
void tick(float deltaTime);
void undoMovement();
private:
Texture2D texture{LoadTexture("characters/knight_idle_spritesheet.png")};
Texture2D idle{LoadTexture("characters/knight_idle_spritesheet.png")};
Texture2D run{LoadTexture("characters/knight_run_spritesheet.png")};
Vector2 screenPos{};
Vector2 worldPos{};
Vector2 worldPosLastFrame{};
// 1 : facing right, -1 : facing left
float rightLeft{1.f};
// animation variables
float runningTime{};
int frame{};
int maxFrames{6};
float updateTime{1.f / 12.f};
float speed{4.f};
float width{};
float height{};
float scale{4.0f};
};
and my character.cpp
like this:
#include "character.h"
#include "raylib.h"
#include "raymath.h"
Character::Character(int winWidth, int winHeight)
{
width = texture.width / maxFrames;
height = texture.height;
screenPos = {static_cast<float>(winWidth) / 2.0f - scale * (0.5f * width),
static_cast<float>(winHeight) / 2.0f - scale * (0.5f * height)};
}
void Character::tick(float deltaTime)
{
worldPosLastFrame = worldPos;
Vector2 direction{};
if (IsKeyDown(KEY_A))
direction.x -= 1.0;
if (IsKeyDown(KEY_D))
direction.x += 1.0;
if (IsKeyDown(KEY_W))
direction.y -= 1.0;
if (IsKeyDown(KEY_S))
direction.y += 1.0;
if (Vector2Length(direction) != 0.0)
{
// set worldPos = worldPos + direction
worldPos = Vector2Add(worldPos, Vector2Scale(Vector2Normalize(direction), speed));
direction.x < 0.f ? rightLeft = -1.f : rightLeft = 1.f;
texture = run;
}
else
{
texture = idle;
}
// update animation frame
runningTime += deltaTime;
if (runningTime >= updateTime)
{
frame++;
runningTime = 0.f;
if (frame > maxFrames)
frame = 0;
}
// draw the character
Rectangle source{frame * width, 0.f, rightLeft * width, height};
Rectangle dest{screenPos.x, screenPos.y, scale * width, scale * height};
DrawTexturePro(texture, source, dest, Vector2{}, 0.f, WHITE);
}
void Character::undoMovement()
{
worldPos = worldPosLastFrame;
}
When I compile I get the following errors in character.cpp
; “no instance of overloaded function “Character::Character” matches the specified type” on the Character::Character(int winWidth, int winHeight)
line and “identifier “scale” is undefined” on every use of scale
in it. For some reason, it seems that character.cpp
is not accepting the new changes in character.h
.
The result is that Character knight{windowWidth, windowHeight};
does not work in main.cpp
. Anyone have any idea why this is happening? I’ve used text comparison tools with the gitlab additions from resources, and found it to be nearly identical to the class code supplied. I am, however, using g++ to compile on ubuntu, which is my only real deviation form the classes. However, up to this lesson, it has worked.