SFML community forums

Help => Graphics => Topic started by: lukaius on December 06, 2017, 11:52:56 pm

Title: Screen only displays movement when mouse is moving
Post by: lukaius on December 06, 2017, 11:52:56 pm
Hi, when I try to move a sprite by the key board but I have to move the mouse for it to move.

Any help? The code is below
#include <SFML/Graphics.hpp>
void initShape(sf::RectangleShape& shape, sf::Vector2f const& pos, sf::Color const& color){
shape.setFillColor(color);
shape.setPosition(pos);
shape.setOrigin(shape.getSize()*0.5f);}
int main()
{
sf::RenderWindow window(sf::VideoMode(480, 180), "My window");
window.setFramerateLimit(60);
sf::Vector2f startPos = sf::Vector2f(50, 50);
sf::RectangleShape playerRect(sf::Vector2f(50, 50));
initShape(playerRect, startPos, sf::Color::Green);
sf::RectangleShape targetrect(sf::Vector2f(50,50));
initShape(targetrect, sf::Vector2f(400, 50), sf::Color::Yellow);
sf::RectangleShape Badrect(sf::Vector2f(50, 100));
initShape(Badrect, sf::Vector2f(250, 50), sf::Color::Red);

while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed){
window.close();}
playerRect.move(1,0);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){playerRect.move(0,1);}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){playerRect.move(0,-1);}
if(playerRect.getGlobalBounds().intersects(targetrect.getGlobalBounds())){window.close();}}
if(playerRect.getGlobalBounds().intersects(Badrect.getGlobalBounds())){playerRect.setPosition(startPos);}
window.clear(sf::Color::Black);
window.draw(playerRect);
window.draw(Badrect);
window.draw(targetrect);
window.display();}
return 0;
}
Title: Re: Screen only displays movement when mouse is moving
Post by: achpile on December 07, 2017, 07:03:53 am
Why do you write code like this? Have you ever took a look at code formatting on some other projects?

window.close();}
playerRect.move(1,0);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){playerRect.move(0,1);}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){playerRect.move(0,-1);}
if(playerRect.getGlobalBounds().intersects(targetrect.getGlobalBounds())){window.close();}}

window.close();}}
playerRect.move(1,0);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){playerRect.move(0,1);}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){playerRect.move(0,-1);}
if(playerRect.getGlobalBounds().intersects(targetrect.getGlobalBounds())){window.close();}
Title: Re: Screen only displays movement when mouse is moving
Post by: lukaius on December 07, 2017, 01:24:13 pm
Well, I kinda use my own style format like this
void see this(args){if(6==7){
}
}
 
And yes I've looked at other projects it get confusing sometimes in their format
Title: Re: Screen only displays movement when mouse is moving
Post by: eXpl0it3r on December 07, 2017, 02:40:43 pm
You should adapt to a more common style, otherwise you'll always have trouble reading any code and everyone will have trouble reading yours.
Title: Re: Screen only displays movement when mouse is moving
Post by: achpile on December 07, 2017, 03:43:33 pm
Well, I kinda use my own style format like this
void see this(args){if(6==7){
}
}
 
And yes I've looked at other projects it get confusing sometimes in their format

You can't find this error by yourself only because this kind of formatting.
Title: Re: Screen only displays movement when mouse is moving
Post by: lukaius on December 07, 2017, 03:58:55 pm
Okay! I have a source formatting plugin for code blocks IDE. first I'll use my format then i will use the formatting plugin when I release  it or ask for help.
Also, Is it impossible for you to answer my question because of its format it!?
Title: Re: Screen only displays movement when mouse is moving
Post by: achpile on December 07, 2017, 04:04:06 pm
Okay! I have a source formatting plugin for code blocks IDE. first I'll use my format then i will use the formatting plugin when I release  it or ask for help.
Also, Is it impossible for you to answer my question because of its format it!?

I already answered your question in my first post here. I've posted your wrong part and fixed part
Title: Re: Screen only displays movement when mouse is moving
Post by: lukaius on December 07, 2017, 04:11:08 pm
i see now thanks you answered my question. I can see the importance of formatting now!