Welcome, Guest. Please login or register.

Author Topic: Screen only displays movement when mouse is moving  (Read 261 times)

0 Members and 1 Guest are viewing this topic.

lukaius

  • Guest
Screen only displays movement when mouse is moving
« 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;
}

achpile

  • Full Member
  • ***
  • Posts: 220
    • View Profile
    • Achpile's homepage
    • Email
Re: Screen only displays movement when mouse is moving
« Reply #1 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();}

lukaius

  • Guest
Re: Screen only displays movement when mouse is moving
« Reply #2 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 8997
    • View Profile
    • development blog
    • Email
Re: Screen only displays movement when mouse is moving
« Reply #3 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

achpile

  • Full Member
  • ***
  • Posts: 220
    • View Profile
    • Achpile's homepage
    • Email
Re: Screen only displays movement when mouse is moving
« Reply #4 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.

lukaius

  • Guest
Re: Screen only displays movement when mouse is moving
« Reply #5 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!?

achpile

  • Full Member
  • ***
  • Posts: 220
    • View Profile
    • Achpile's homepage
    • Email
Re: Screen only displays movement when mouse is moving
« Reply #6 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

lukaius

  • Guest
Re: Screen only displays movement when mouse is moving
« Reply #7 on: December 07, 2017, 04:11:08 pm »
i see now thanks you answered my question. I can see the importance of formatting now!