Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML 2.0 Keyrelease help  (Read 2281 times)

0 Members and 1 Guest are viewing this topic.

Linux Vs God

  • Newbie
  • *
  • Posts: 18
    • View Profile
SFML 2.0 Keyrelease help
« on: August 09, 2011, 05:02:36 am »
Ok, I am making a Zelda game similar to a Link To The Past. I am trying to make my character animation's image default to 0 whenever i release one of the direction buttons on my keyboard. This works about 85% of the time. Is there something wrong with this code?

Code: [Select]
void Character::Movement()
{
if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Up))
{
currentView = UP;
spriteFrame++;

if(spriteFrame >= 9)
spriteFrame = 0;

for(int i=0; i < spriteFrame; ++i)
LinkSprite.SetImage(linkup[i], false);

LinkSprite.Move(0.f, -speed);
}
else if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Down))
{
currentView = DOWN;
spriteFrame++;

if(spriteFrame >= 9)
spriteFrame = 0;

for(int i=0; i < spriteFrame; ++i)
LinkSprite.SetImage(linkdown[i], false);

LinkSprite.Move(0.f, speed);
}
else if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Right))
{
currentView = RIGHT;
spriteFrame++;

if(spriteFrame >= 9)
spriteFrame = 0;

for(int i=0; i < spriteFrame; ++i)
LinkSprite.SetImage(linkright[i], true);

LinkSprite.Move(speed, 0.f);
}
else if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Left))
{
currentView = LEFT;
spriteFrame++;

if(spriteFrame >= 9)
spriteFrame = 0;

for(int i=0; i < spriteFrame; ++i)
LinkSprite.SetImage(linkleft[i], true);

LinkSprite.Move(-speed, 0.f);
}
}

void Character::KeyRelease()
{
if(sf::Event::KeyReleased && sf::Keyboard::Right && currentView == RIGHT)
LinkSprite.SetImage(linkright[0]);
if(sf::Event::KeyReleased && sf::Keyboard::Left && currentView == LEFT)
LinkSprite.SetImage(linkleft[0]);
if(sf::Event::KeyReleased && sf::Keyboard::Up && currentView == UP)
LinkSprite.SetImage(linkup[0]);
if(sf::Event::KeyReleased && sf::Keyboard::Down && currentView == DOWN)
LinkSprite.SetImage(linkdown[0]);
}

Linux Vs God

  • Newbie
  • *
  • Posts: 18
    • View Profile
SFML 2.0 Keyrelease help
« Reply #1 on: August 09, 2011, 08:10:33 pm »
Anybody got ideas?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SFML 2.0 Keyrelease help
« Reply #2 on: August 09, 2011, 08:30:55 pm »
There are many mistakes in your code, maybe you should read carefully the tutorials and documentation.

Code: [Select]
if(sf::Event::KeyReleased && sf::Keyboard::Right && currentView == RIGHT)
Here you are testing a constant expression, sf::Event::KeyReleased and sf::Keyboard::Right are numeric constants, not boolean conditions. It's like you wrote "if (5 && 24 && currentView == RIGHT)".

Code: [Select]
for(int i=0; i < spriteFrame; ++i)
         LinkSprite.SetImage(linkleft[i], true);

You're changing the sprite's image several times, but only the last assignment will be taken into account. I don't know what this code was supposed to do ;)

When is Character::KeyRelease() called? Why don't you just write a final "else" in Character::Movement() to handle the case when no direction key is pressed?
Laurent Gomila - SFML developer

Linux Vs God

  • Newbie
  • *
  • Posts: 18
    • View Profile
SFML 2.0 Keyrelease help
« Reply #3 on: August 09, 2011, 08:44:33 pm »
It works 100% now thanks

 

anything