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

Author Topic: Sprite is not moving  (Read 985 times)

0 Members and 1 Guest are viewing this topic.

nopilpl

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Sprite is not moving
« on: January 23, 2018, 09:52:28 pm »
Hey
I have problem
#define NOMINMAX
#define SFML_NO_DEPRECATED_WARNINGS
#include <iostream>
#include <Windows.h>
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include <SFML\Audio.hpp>
#include <SFML\Window.hpp>
#include <algorithm>

using namespace sf;
using namespace std;

int main()
{
        Sprite sgracz; //sprite
        //Sprite smapa1;
        Texture gracz;
        //Texture mapa1;

        gracz.loadFromFile("Textures/Player.png");
        sgracz.setTexture(gracz);
        sgracz.setPosition(Vector2f(120, 200));


        enum enumStates { MENU, GAME, GAMEOVER, END };
        enumStates state = MENU;
        RenderWindow window(VideoMode(1500, 900), "Rise of the Cyborg Story");
        while (window.isOpen())
        {              
                Font font;

                if (!font.loadFromFile("Fonts/arial.ttf"))
                {
                        //Error
                }

                ////// Menu //////

                // Nag&#322;ówek //
                Text title("Rise of the Cyborg Story", font, 80);
                title.setStyle(Text::Bold);
                title.setPosition(1280 / 2 - title.getGlobalBounds().width / 2, 20);

                Text Play;
                Text Options;
                Text Exit;

                // Play //
                Play.setFont(font);
                Play.setCharacterSize(65);
                Play.setString("Play");
                Play.setPosition(1280 / 2 - Play.getGlobalBounds().width / 2, 250);

                // Options //
                Options.setFont(font);
                Options.setCharacterSize(65);
                Options.setString("Options");
                Options.setPosition(1280 / 2 - Options.getGlobalBounds().width / 2, 250 + 120);

                // Exit //
                Exit.setFont(font);
                Exit.setCharacterSize(80);
                Exit.setString("Exit");
                Exit.setPosition(1280 / 2 - Exit.getGlobalBounds().width / 2, 250 + 240);

                //// Dzia&#322;anie menu ////
                Vector2f mouse(Mouse::getPosition(window));
                Event event;
                int selected = 1;

                // Wy&#322;&#261;czanie gry //
                while (window.pollEvent(event))
                {

                        if (event.type == Event::Closed)
                        {
                                window.close();
                        }

                        else if (Exit.getGlobalBounds().contains(mouse) &&
                                event.type == Event::MouseButtonReleased && event.key.code == Mouse::Left)
                        {
                                window.close();
                        }
                }


                Text wybrane1;
                Text wybrane2;

                // Pod&#347;wietlanie si&#281; napisów w menu //
                if (Play.getGlobalBounds().contains(mouse))
                        Play.setColor(Color::Cyan);
                else (Play.setColor(Color::White));

                if (Options.getGlobalBounds().contains(mouse))
                        Options.setColor(Color::Cyan);
                else (Options.setColor(Color::White));

                if (Exit.getGlobalBounds().contains(mouse))
                        Exit.setColor(Color::Cyan);
                else (Exit.setColor(Color::White));

                if (state == MENU)
                {
                        window.draw(title);
                        window.draw(Play);
                        window.draw(Options);
                        window.draw(Exit);
                }

                ////// Koniec Menu //////

                if (Play.getGlobalBounds().contains(mouse) && event.type == Event::MouseButtonPressed && event.key.code == Mouse::Left)
                {
                        state = GAME;
                }


                if (state == GAME)
        {
                        window.clear();

                        Event poruszanieSie;
                        while (window.pollEvent(poruszanieSie))
                        {
                                if ( poruszanieSie.type == Event::KeyPressed && poruszanieSie.key.code == Keyboard::Right)
                                {
                                        sgracz.move(Vector2f( 50, 0));
                                       
                                }

                                if (poruszanieSie.type == Event::KeyPressed && poruszanieSie.key.code == Keyboard::Left)
                                {
                                        sgracz.move(Vector2f(-50, 0));
                                       
                                }
                        }
                        window.draw(sgracz);
                }
                window.display();
        }

        return EXIT_SUCCESS;
}
 
So I want to move my sprite when i will click button but it doesnt work
« Last Edit: January 23, 2018, 09:59:21 pm by nopilpl »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Sprite is not moving
« Reply #1 on: January 23, 2018, 11:14:29 pm »
Your game loop has a few different issues that could probably be fixed with some better code organization. The problem you're asking about, though, may likely be due to having multiple pollEvent loops.  The first loop will pop all of the events from the event queue, so there will be no events left in the queue for when your second loop runs. You should only make one loop that calls pollEvent.

 

anything