-
Hello! So I have this most simple project in which I have 2 events: when "W" is pressed and when "S". As you can see there are 3 lines for where must be square after pressing buttons. BUT! It works only like total fall to the 3nd line or total up to the 1nd line AND(!) if you press buttons quickly you can see that 2nd line actually is present but code skips it. I can't understand why, please help. Maybe my VS is broken?
UPDATE* He-he I forgot to add a project ;D
https://www.dropbox.com/s/7cia15c6lto9600/Test.rar?dl=0
-
Please don't post a link to an archive, put the relevant code directly here.
-
#include <SFML/Graphics.hpp>
using namespace sf;
using namespace std;
String CurrentlyAt = "Menu";
int Lal = 1;
int main()
{
RenderWindow window(VideoMode(1244, 772), "Test");
Texture TextureMenu;
TextureMenu.loadFromFile("Menu.png");
Sprite Menu(TextureMenu);
Texture TextureOptionsInMenu;
TextureOptionsInMenu.loadFromFile("1.png");
Sprite OptionsInMenu(TextureOptionsInMenu);
OptionsInMenu.setPosition(487, 293);
while (window.isOpen())
{
// Process events
Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == Event::Closed)
window.close();
}
if (Keyboard::isKeyPressed(Keyboard::Key::W))
{
if (CurrentlyAt == "Menu")
{
if (Lal == 2)
{
OptionsInMenu.setPosition(487, 293);
Lal = 1;
}
else if (Lal == 1)
{
OptionsInMenu.setPosition(487, 236);
Lal = 0;
}
}
}
if (Keyboard::isKeyPressed(Keyboard::Key::S))
{
if (CurrentlyAt == "Menu")
{
if (Lal == 0)
{
OptionsInMenu.setPosition(487, 293);
Lal = 1;
}
else if (Lal == 1)
{
OptionsInMenu.setPosition(487, 348);
Lal = 2;
}
}
}
window.clear();
window.draw(Menu);
window.draw(OptionsInMenu);
window.display();
}
return 0;
}
-
Please don't post a link to an archive, put the relevant code directly here.
So I added a code but I do not think that there is any problem with it. It just goes through both "if" when you press "W" or "S". Btw, if you make breakpoint it will crush cus even VS understand that if you chose one of the IFs you can't go to others.
Such a lol.
Try this code please anyway. I still think that my VS is broken.
-
Code works as expected on my end. What do you want the code to do?
-
Code works as expected on my end. What do you want the code to do?
Start program, the square must be on the 2 line. Press W, the square must be on the 1 line. Then press S, the square must be in the 2 line again BUT in MY VS it goes to 3 line. And then I press W again it goes to 1 line. and etc. etc. etc. 2 line is never achieved. Soooo. Try again. And I will grateful for screenshots.
-
Your game loop will go as fast as possible or if VSync is enforced at 60fps, that is around 16.66ms. Now if you hold your key down for longer than 16.66ms (which you most likely do) the loop will execute at least twice and the the variable will be set.
If you want to "react" to key presses, it's recommended to use events instead.
-
Your game loop will go as fast as possible or if VSync is enforced at 60fps, that is around 16.66ms. Now if you hold your key down for longer than 16.66ms (which you most likely do) the loop will execute at least twice and the the variable will be set.
If you want to "react" to key presses, it's recommended to use events instead.
Oh so it is all about time. Can you please provide me the code with events for this project? I want my buttons to trigger only once when it is pressed.
Thx for the answer!!!
-
See the official tutorial and documentation.
https://www.sfml-dev.org/learn.php
-
See the official tutorial and documentation.
https://www.sfml-dev.org/learn.php
I did it, thx!
Here is what i got in the end.
#include <SFML/Graphics.hpp>
using namespace sf;
using namespace std;
String CurrentlyAt = "Menu";
int Lal = 1;
int main()
{
RenderWindow window(VideoMode(1244, 772), "Test");
Texture TextureMenu;
TextureMenu.loadFromFile("Menu.png");
Sprite Menu(TextureMenu);
Texture TextureOptionsInMenu;
TextureOptionsInMenu.loadFromFile("1.png");
Sprite OptionsInMenu(TextureOptionsInMenu);
OptionsInMenu.setPosition(487, 293);
while (window.isOpen())
{
window.clear();
// Process events
Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed)
{
if (Keyboard::isKeyPressed(Keyboard::Key::W))
{
if (CurrentlyAt == "Menu")
{
if (Lal == 2)
{
OptionsInMenu.setPosition(487, 293);
Lal = 1;
}
else if (Lal == 1)
{
OptionsInMenu.setPosition(487, 236);
Lal = 0;
}
}
}
if (Keyboard::isKeyPressed(Keyboard::Key::S))
{
if (CurrentlyAt == "Menu")
{
if (Lal == 0)
{
OptionsInMenu.setPosition(487, 293);
Lal = 1;
}
else if (Lal == 1)
{
OptionsInMenu.setPosition(487, 348);
Lal = 2;
}
}
}
}
}
window.draw(Menu);
window.draw(OptionsInMenu);
window.display();
}
return 0;
}
-
That is wrong, check the event tutorial again.
Also use [code=cpp][/code] tag when posting code to the forum.
-
That is wrong, check the event tutorial again.
Also use [code=cpp][/code] tag when posting code to the forum.
#include <SFML/Graphics.hpp>
using namespace sf;
using namespace std;
String CurrentlyAt = "Menu";
int Lal = 1;
int main()
{
RenderWindow window(VideoMode(1244, 772), "Test");
Texture TextureMenu;
TextureMenu.loadFromFile("Menu.png");
Sprite Menu(TextureMenu);
Texture TextureOptionsInMenu;
TextureOptionsInMenu.loadFromFile("1.png");
Sprite OptionsInMenu(TextureOptionsInMenu);
OptionsInMenu.setPosition(487, 293);
window.setKeyRepeatEnabled(false);
while (window.isOpen())
{
window.clear();
// Process events
Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
// window closed
case sf::Event::Closed:
window.close();
break;
// key pressed
case sf::Event::KeyPressed:
if (Keyboard::isKeyPressed(Keyboard::Key::W))
{
if (CurrentlyAt == "Menu")
{
if (Lal == 2)
{
OptionsInMenu.setPosition(487, 293);
Lal = 1;
}
else if (Lal == 1)
{
OptionsInMenu.setPosition(487, 236);
Lal = 0;
}
}
}
if (Keyboard::isKeyPressed(Keyboard::Key::S))
{
if (CurrentlyAt == "Menu")
{
if (Lal == 0)
{
OptionsInMenu.setPosition(487, 293);
Lal = 1;
}
else if (Lal == 1)
{
OptionsInMenu.setPosition(487, 348);
Lal = 2;
}
}
}
break;
// we don't process other types of events
default:
break;
}
}
window.draw(Menu);
window.draw(OptionsInMenu);
window.display();
}
return 0;
}
Thx for spending your time on me ;D
I hope this is the right one.
-
Whether you use if/else or switch is not really important, what is important, is that you need to check the event's key and not use isKeyPressed. Again read the tutorial on events (https://www.sfml-dev.org/tutorials/2.4/window-events.php#the-keypressed-and-keyreleased-events), especially the big red box! ;)
-
Whether you use if/else or switch is not really important, what is important, is that you need to check the event's key and not use isKeyPressed. Again read the tutorial on events (https://www.sfml-dev.org/tutorials/2.4/window-events.php#the-keypressed-and-keyreleased-events), especially the big red box! ;)
I'm so thankful to you for spending your time on me ;D
Soo this is the very last solution that I found.
#include <SFML/Graphics.hpp>
using namespace sf;
using namespace std;
String CurrentlyAt = "Menu";
int Lal = 1;
int main()
{
RenderWindow window(VideoMode(1244, 772), "Test");
Texture TextureMenu;
TextureMenu.loadFromFile("Menu.png");
Sprite Menu(TextureMenu);
Texture TextureOptionsInMenu;
TextureOptionsInMenu.loadFromFile("1.png");
Sprite OptionsInMenu(TextureOptionsInMenu);
OptionsInMenu.setPosition(487, 293);
window.setKeyRepeatEnabled(false);
while (window.isOpen())
{
window.clear();
// Process events
Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
// window closed
case sf::Event::Closed:
window.close();
break;
// key pressed
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::W)
{
if (CurrentlyAt == "Menu")
{
if (Lal == 2)
{
OptionsInMenu.setPosition(487, 293);
Lal = 1;
}
else if (Lal == 1)
{
OptionsInMenu.setPosition(487, 236);
Lal = 0;
}
}
}
else if (event.key.code == sf::Keyboard::S)
{
if (CurrentlyAt == "Menu")
{
if (Lal == 0)
{
OptionsInMenu.setPosition(487, 293);
Lal = 1;
}
else if (Lal == 1)
{
OptionsInMenu.setPosition(487, 348);
Lal = 2;
}
}
}
break;
// we don't process other types of events
default:
break;
}
}
window.draw(Menu);
window.draw(OptionsInMenu);
window.display();
}
return 0;
}