Bonjours a vous, Apres avoir suivis un tuto pour débuter la sfml, j’ai décider d’animer un petit personnage, le fessant bouger avec les touches ZQSD, tous allait bien quand tous le code ce trouvait dans le fichier main, mais après avoir creer une class "player" heritant de sf::Sprit, mon petit personnage disparait a la pression des touches Q & Z, j’ai tester de supprimer le setTextureRect, le changement de direction, la verification de hors map, l’animation, rien n’y fait Le code : main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #include <SFML/Graphics.hpp> #include <iostream> #include <array> #include "player.h" #pragma region define #define SPEED 10 #pragma endregion define #pragma region prototype void Management_Keyboard(Player &target); #pragma endregion prototype int main() { int screenW(640); //Largeur fenetre int screenH(480); //Hauteur fenetre const size_t tileSize(48); sf::Vector2f positionPlayer; sf::View view; Player player(tileSize); sf::RenderWindow window(sf::VideoMode(screenW, screenH, 32), "Test ^.^"); //Creation de la fenetre window.setFramerateLimit(20); window.setVerticalSyncEnabled(true); view.reset(sf::FloatRect(0, 0, screenW, screenH)); sf::RectangleShape rect; rect.setSize(sf::Vector2f(tileSize, tileSize)); rect.setFillColor(sf::Color::Red); rect.setPosition(100, 100); while (window.isOpen()) //Boucle evenement { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { window.close(); } } Management_Keyboard(player); positionPlayer.x = player.getPosition().x + (tileSize / 2) - (screenW / 2); positionPlayer.y = player.getPosition().y + (tileSize / 2) - (screenH / 2); if (positionPlayer.x < 0) { positionPlayer.x = 0; } view.reset(sf::FloatRect(positionPlayer.x, positionPlayer.y, screenW, screenH)); window.setView(view); window.clear(); //Efface la fenetre window.draw(player); window.draw(rect); std::cout << "Dessine le rect" << std::endl; std::cout << "Dessine le player" << std::endl; window.display(); //Affiche la fenetre } return 0; } void Management_Keyboard(Player &target){ if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) { target.moove(sf::Vector2u(0, -SPEED)); } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { target.moove(sf::Vector2u(0, SPEED)); } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) { target.moove(sf::Vector2u(-SPEED, 0)); } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { target.moove(sf::Vector2u(SPEED, 0)); } } |
player.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #ifndef PLAYER_H_INCLUED #define PLAYER_H_INCLUDED #include <SFML\Graphics.hpp> class Player : public sf::Sprite { public: Player(size_t tileSize); void update(bool update); void moove(sf::Vector2u movement); void inMap(); ~Player(); private: size_t _tileSize; enum _direction { Down, Left, Right, Up }; sf::Vector2i _animation; sf::Texture _texturePlayer; }; #endif |
player.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include "player.h" #include <iostream> Player::Player(size_t tileSize) : _animation(1, Down), _tileSize(tileSize) { if (!_texturePlayer.loadFromFile("Ressource/Sprite/Player.bmp")) { std::cout << "Load texture player failed" << std::endl; } setTexture(_texturePlayer); setPosition(0, 0); Player::update(false); } void Player::update(bool update) { if (update == true) { _animation.x++; if (_animation.x * _tileSize >= _texturePlayer.getSize().x) { _animation.x = 0; } } setTextureRect(sf::IntRect(_animation.x * _tileSize, _animation.y * _tileSize, _tileSize, _tileSize)); std::cout << "Je suis mis a jour" << std::endl; } void Player::moove(sf::Vector2u movement) { if (movement.x == 0) { if (movement.y < 0) { _animation.y = Up; } if (movement.y > 0) { _animation.y = Down; } } if (movement.y == 0) { if (movement.x < 0) { _animation.y = Left; } if (movement.x > 0) { _animation.y = Right; } } move(movement.x, movement.y); std::cout << "Je bouge" << std::endl; Player::inMap(); Player::update(true); } void Player::inMap() { if (getPosition().x < 0) { setPosition(0, getPosition().y); } else { std::cout << "Je suis dans la map" << std::endl; } } Player::~Player() { } ` |
(NOTE : - Le sf::RectangleShape disparait en même temps que le personnage - Tous les std::cout sont des verifications, et tous sont present dans la console (hormis pour le fail de chargement de texture) )
Voila, ceci est mon premier post sur ce forum, je suis debutant et preneur de toute remarque et conseil
Merci d’avance pour votre aide
(EDIT : Code clarifier)