Hey all,
So I'm working on the basics of a 2D zelda-style game (as a hobby) and I've got a question about the event system.
The problem is that when I hold any movement key (WASD), my sprite animation will pause for a short period of time and then act as it should, updating the spriteRect every .1 second.
I took the problem to the console, making it print every time an event was detected, every time the animation function was called, and so on, to figure out what was causing it to pause.
I found that this happens for all events detected including ones I don't handle (like the mouse) and it's the event loop that's skipped whenever an event is held.
When I just press a movement key, it acts as it should and moves the spriteRect for a split second, then resets. The problem is only when an event is held down.
Here's the segments of code that matter-
level1.cpp:
gameWindow.setFramerateLimit(30);
while (gameWindow.isOpen())
{
sf::Event event;
while (gameWindow.pollEvent(event))
{
if (event.type == sf::Event::Closed)
gameWindow.close();
player->handle_input(event, clock);
std::cout << "event detected" << std::endl;
enemy->path(*player, clock);
}
enemy->move();
player->move();
player.cpp handle_input function:
void Player::handle_input(sf::Event event, sf::Clock& clock)
{
switch(event.type)
{
case sf::Event::KeyPressed:
{
switch(event.key.code)
{
case sf::Keyboard::W:
{
velocity.y = -1;
direction(2);
if(clock.getElapsedTime().asSeconds() > 0.1f)
{
walking();
clock.restart();
}
break;
}
case sf::Keyboard::A:
{
velocity.x = -1;
direction(3);
if(clock.getElapsedTime().asSeconds() > 0.1f)
{
walking();
clock.restart();
}
break;
}
case sf::Keyboard::S:
{
velocity.y = 1;
direction(1);
if(clock.getElapsedTime().asSeconds() > 0.1f)
{
walking();
clock.restart();
}
break;
}
case sf::Keyboard::D:
{
velocity.x = 1;
direction(4);
if(clock.getElapsedTime().asSeconds() > 0.1f)
{
walking();
clock.restart();
}
break;
}
}
break;
}
case sf::Event::KeyReleased:
{
switch(event.key.code)
{
case sf::Keyboard::W: velocity.y = 0; break;
case sf::Keyboard::S: velocity.y = 0; break;
case sf::Keyboard::A: velocity.x = 0; break;
case sf::Keyboard::D: velocity.x = 0; break;
}
break;
}
}
if(velocity.x == 0 && velocity.y == 0)
{
spriteRect.left = 0;
msprite.setTextureRect(spriteRect);
}
}
player.cpp move():
void Player::move()
{
position.x +=velocity.x;
if(position.x < 0 || position.x > (wSize.x-19))
position.x -= velocity.x;
position.y += velocity.y;
if(position.y < 0 || position.y > (wSize.y-25))
position.y -= velocity.y;
msprite.setPosition(position);
box = msprite.getGlobalBounds();
}
animate.cpp walking()
void Animate::walking()
{
if (spriteRect.left == (mdimensions.x * 3))
spriteRect.left = 0;
else
spriteRect.left +=mdimensions.x;
msprite.setTextureRect(spriteRect);
}
animate.cpp direction function:
void Animate::direction(int direction)
{
switch(direction)
{
case 1: spriteRect.top = 0; break;
case 2: spriteRect.top = mdimensions.y;break;
case 3: spriteRect.top = (mdimensions.y*2);break;
case 4: spriteRect.top = (mdimensions.y*3);break;
}
msprite.setTextureRect(spriteRect);
}
The sprite DOES move smoothly, with no pause, it's just the animation that pauses for a little bit.
All I'm looking for here is to get rid of that weird intial pause when events are held down and have smooth animation.
I'm about to head out to go to work, but when I get back I'll be able to respond.