midway after changing the different variables to the common variable “Velocity” and moving the relevant code into BaseCharacter.cpp, my enemy moves towards character, but I can’t move the character. He stays still in his original position. I am not showing errors in the code, but I can’t move. Any suggestions?
From BaseCharacter.cpp
void BaseCharacter::tick(float deltaTime)
{
worldPosLastFrame = worldPos;
// update animation frame
runningTime += deltaTime;
if (runningTime >= updateTime)
{
frame++;
runningTime = 0.f;
if (frame > maxFrames)
frame = 0;
}
if (Vector2Length(velocity) != 0.0)
{
// set worldPos = worldPos + direction
worldPos = Vector2Add(worldPos, Vector2Scale(Vector2Normalize(velocity), speed));
velocity.x < 0.f ? rightLeft = -1.f : rightLeft = 1.f;
texture = run;
}
else
{
texture = idle;
}
velocity = {};
// 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);
// stop drawing
From character.cpp
#include “Character.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)
{
Vector2 velocity{};
if (IsKeyDown(KEY_A))
velocity.x -= 1.0;
if (IsKeyDown(KEY_D))
velocity.x += 1.0;
if (IsKeyDown(KEY_W))
velocity.y -= 1.0;
if (IsKeyDown(KEY_S))
velocity.y += 1.0;
BaseCharacter::tick(deltaTime);
}