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

Author Topic: Help with character move  (Read 1109 times)

0 Members and 1 Guest are viewing this topic.

smiaro

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Help with character move
« on: December 16, 2013, 09:15:31 am »
Hello, I just watched SpriteSheet Animation tutorial and tried to put that code in my project I have downloaded character from famitsu generator. But when I press Up, down my character doesn't move. He is still turn left, why ?:)

Game.cpp - menuStates switch code:
case Game::Single:
                        {
                                sf::Event zdarzenie;
                                while (oknoAplikacji.pollEvent(zdarzenie))
                                {
                                       
                                        Level level;
                                        level.Zaladuj(oknoAplikacji);
                                        if (zdarzenie.type == sf::Event::Closed) stanGry = Game::Wychodzenie;

                                        if (zdarzenie.type == sf::Event::KeyPressed)
                                        {
                                                if (zdarzenie.key.code == sf::Keyboard::Escape) PokazMenu();
                                        }
                                }

                                break;
                        }
 

Player class code:
#include <SFML/Graphics.hpp>
#include "Player.h"

void Player::Rysuj(sf::RenderWindow & oknoAplikacji)
{
        enum Kierunek { Lewo, Prawo, Gora, Dol }; //{0,1,2,3}
        sf::Vector2i source(1, Dol);

        oknoAplikacji.clear(sf::Color(0, 0, 0));
        sf::Texture obrazGracza;
        obrazGracza.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/player.png");
        sf::Sprite player(obrazGracza);
        player.setPosition(0, 0);

        while (oknoAplikacji.isOpen()) {
                sf::Event zdarzenie;
                while (oknoAplikacji.pollEvent(zdarzenie)) {
                        switch (zdarzenie.type) {
                        case sf::Event::KeyPressed:
                                if (zdarzenie.key.code == sf::Keyboard::Left)
                                        source.y = Lewo;
                                else if (zdarzenie.key.code == sf::Keyboard::Right)
                                        source.y == Prawo;
                                else if (zdarzenie.key.code == sf::Keyboard::Up)
                                        source.x == Gora;
                                else if (zdarzenie.key.code == sf::Keyboard::Down)
                                        source.x == Dol;
                                break;
                        }
                }
                player.setTextureRect(sf::IntRect(source.x = 32, source.y = 32, 32, 32));
                oknoAplikacji.draw(player);
                oknoAplikacji.display();
                oknoAplikacji.clear();
        }
}
 

In my Level::Zaladuj method I have player.Rysuj();
Please help me :)
« Last Edit: December 16, 2013, 09:47:07 am by smiaro »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Help with character move
« Reply #1 on: December 16, 2013, 10:21:37 am »
I highly recommend to write code in English, especially if you try to get help in an English oriented community - it makes it painfully hard for us to understand code with foreign languages in it... :-\

Your cause of the issue is most likely in this line:
player.setTextureRect(sf::IntRect(source.x = 32, source.y = 32, 32, 32));

Why would you assign 32 to x and y WITHIN an initialization call for IntRect? :o
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything