Welcome,
Guest
. Please
login
or
register
. Did you miss your
activation email?
French forum
Home
Help
Search
Login
Register
SFML community forums
»
Help
»
Window
»
Event::KeyPressed not working now?
Print
Pages: [
1
]
Author
Topic: Event::KeyPressed not working now? (Read 2585 times)
0 Members and 1 Guest are viewing this topic.
expedo
Newbie
Posts: 3
Event::KeyPressed not working now?
«
on:
June 04, 2019, 03:27:10 am »
Im doing this simple project where you can control and make the character jump, but whenever I do
if (event.type == Event::KeyPressed)
it wont execute the code in it when I press a key. Though this worked fine a while ago for some reason and my unfinished code is this:
(click to show/hide)
#include "pch.h"
#include <iostream>
#include <SFML/Graphics.hpp>
#include "player.h"
using namespace std;
using namespace sf;
int main()
{
float gravity = 1;
Vector2f window_size(800, 600);
RenderWindow window(VideoMode(window_size.x, window_size.y), "simulation");
Vector2f starting_pos(385, 550);
Player plr(starting_pos);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type = Event::Closed)
{
window.close();
}
if (event.type == Event::KeyPressed)
{
cout << "activated" << endl;
if (event.key.code == Keyboard::E)
{
cout << "pressed" << endl;
}
}
}
window.clear();
window.draw(plr.get_character());
window.display();
}
return 0;
}
Logged
FRex
Hero Member
Posts: 1848
Back to C++ gamedev with SFML in May 2023
Re: Event::KeyPressed not working now?
«
Reply #1 on:
June 04, 2019, 06:39:39 am »
if
(
event.
type
=
Event
::
Closed
)
This line is an assignment, not a comparison, and Event::Closed is (usually?) 0 so it'll not close the Window and it prevents handling any other event type because event.type is always Event::Closed.
Logged
Back to C++ gamedev with SFML in May 2023
expedo
Newbie
Posts: 3
Re: Event::KeyPressed not working now?
«
Reply #2 on:
June 04, 2019, 01:24:25 pm »
thanks
Logged
Print
Pages: [
1
]
SFML community forums
»
Help
»
Window
»
Event::KeyPressed not working now?
anything