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

Author Topic: Handling keyPressed event in player's update method (SFML 1.6)  (Read 4188 times)

0 Members and 1 Guest are viewing this topic.

0026sd

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Handling keyPressed event in player's update method (SFML 1.6)
« on: November 06, 2012, 05:00:43 pm »
Hi everyone,

How can I execute code if a key has been pressed inside my Player class' update method?

The issue is that I'm currently using window.GetInput().IsKeyDown(sf::Key::whatever) for the player's movement and jumping but I need only to get the keyPressed when the player fires (so he doesn't spam bullets as long as the space bar is down).

I've tried adding an Event to the method and polling it but it doesn't respond to anything. Please have a look at this simplified code:

Main.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Level.h"

int main()
{
   
    sf::RenderWindow window(sf::VideoMode(500, 300, 32), "SFML KeyPressed");
    window.UseVerticalSync(true);

    Level level;
    level.Initialize();
    level.LoadContent();

    while(window.IsOpened())
    {

        sf::Event Event;
        while(window.GetEvent(Event))
        {

             if(Event.Type == sf::Event::Closed)
                 window.Close();

        }

        level.Update(window);

        window.Clear();

        level.Draw(window);

        window.Display();

    }

    return 0;

}

I won't include the Level class. It basically creates a Player object and calls the player's init, load, update, draw methods in the same way main.cpp does with level.

Player.cpp

// ...

void Player::Update(sf::RenderWindow &window)
{

    // Check for movement
    if(window.GetInput().IsKeyDown(sf::Key::Left))
        movement = -1.0f;
    else if(window.GetInput().IsKeyDown(sf::Key::Right))
        movement = 1.0f;

    // Check for shooting - this is where I only need the key to fire once...
    sf::Event Event;
    while(window.GetEvent(Event))
    {
        if(Event.KeyPressed == sf::Key::Space)
            Shoot();
    }

    // ...

}

// ...

 
« Last Edit: November 06, 2012, 05:03:09 pm by 0026sd »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Handling keyPressed event in player's update method (SFML 1.6)
« Reply #1 on: November 06, 2012, 05:11:01 pm »
Should be
Event.Key.Code
, not
Event.KeyPressed
read the events tutorial. And consider upgrading to 2.0
« Last Edit: November 06, 2012, 05:12:32 pm by FRex »
Back to C++ gamedev with SFML in May 2023

0026sd

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Handling keyPressed event in player's update method (SFML 1.6)
« Reply #2 on: November 06, 2012, 05:19:18 pm »
Should be
Event.Key.Code
, not
Event.KeyPressed
read the events tutorial. And consider upgrading to 2.0

I have read the tutorials and I'm scared to upgrade to 2.0.. haha. Or lazy.

Anyway, thanks for the suggestion but I actually tried that and it didn't work either. The problem is that the tutorials use the events in cooperation with the RenderWindow initialization whereas I want to use it in a separate class from that.
« Last Edit: November 06, 2012, 05:20:53 pm by 0026sd »

 

anything