Bienvenue, Invité. Merci de vous connecter ou de vous inscrire.
Avez-vous perdu votre e-mail d'activation ?

Auteur Sujet: SFML bug de deplacement[resolus]  (Lu 1686 fois)

0 Membres et 1 Invité sur ce sujet

TheYoungGeek43

  • Jr. Member
  • **
  • Messages: 87
    • Voir le profil
SFML bug de deplacement[resolus]
« le: Juillet 22, 2015, 11:23:53 pm »
Bonjour,

Je me suis mis a develloper un jeux avec la SFML sauf que je n'arrive pas a faire deplacer mon personnage
Voici mon code

Main.cpp
#include <SFML\Graphics.hpp>
#include <iostream>
#include "Human.h"
#include "Input.h"
 
using namespace std;
 
int main()
{
   sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Human vs Alien");
   window.setFramerateLimit(60);
 
   Human human;
   Input input;
 
   human.init(window);
   human.setLife(100);
 
   while (window.isOpen())
   {
      input.gestionInputs(window);
      human.update(input);
 
      window.clear();
      human.draw(window);
      window.display();
   }
   return 0;
}

Input.h
#ifndef INPUT_H
#define INPUT_H
 
#include <SFML\Graphics.hpp>
 
class Input
{
   struct Button{ bool LEFT, RIGHT, UP, DOWN, SHOOT; };
public:
   // Constructeur
   Input::Input();
 
   //Accesseur
   Button Input::getButton(void) const;
 
   //Mutateur
   void Input::setButton(int bouton, bool etat);
 
   //Fonction
   void Input::gestionInputs(sf::RenderWindow &window);
   void Input::getInput(sf::RenderWindow &window);
 
 
private:
 
   // Variable de la classe en accès privé
   sf::Event event;
   Button button;
 
   //Enum pour les boutons
   const enum{ UP, DOWN, RIGHT, LEFT, SHOOT };
};
#endif // !INPUT_H

Input.cpp   
#include "Input.h"
 
// Constructeur
Input::Input()
{
   button.LEFT = button.RIGHT = button.UP = button.DOWN = button.SHOOT = false;
}
 
//Accesseur
Input::Button Input::getButton(void) const{ return button; }
 
//Mutateur
void Input::setButton(int bouton, bool etat)
{
   switch (bouton)
   {
   case UP:
      button.UP = etat;
      break;
   case DOWN:
      button.DOWN = etat;
      break;
   case RIGHT:
      button.RIGHT = etat;
      break;
   case LEFT:
      button.LEFT = etat;
      break;
   case SHOOT:
      button.SHOOT = etat;
      break;
   }
}
 
// Fonction
void Input::gestionInputs(sf::RenderWindow &window)
{
   //Pour l'instant, on ne gère que le clavier.
   //On gèrera les joysticks plus tard, s'il y en a
   //un de branché.
   //Pour l'instant, cette fonction n'est donc pas indispensable.
 
   getInput(window);
}
 
void Input::getInput(sf::RenderWindow &window)
{
   while (window.pollEvent(event))
   {
      switch (event.type)
      {
      case sf::Event::Closed:
         window.close();
         break;
 
      case sf::Event::KeyPressed:
         switch (event.key.code)
         {
         case sf::Keyboard::Escape:
            window.close();
            break;
 
         case sf::Keyboard::Left:
            button.LEFT = true;
            break;
 
         case sf::Keyboard::Right:
            button.RIGHT = true;
            break;
 
         case sf::Keyboard::Down:
            button.DOWN = true;
            break;
 
         case sf::Keyboard::Up:
            button.UP = true;
            break;
 
         default:
            break;
         }
         break;
 
      case sf::Event::KeyReleased:
         switch (event.key.code)
         {
         case sf::Keyboard::Left:
            button.LEFT = false;
            break;
 
         case sf::Keyboard::Right:
            button.RIGHT = false;
            break;
 
         case sf::Keyboard::Down:
            button.DOWN = false;
            break;
 
         case sf::Keyboard::Up:
            button.UP = false;
            break;
 
         default:
            break;
         }
         break;
 
         // on ne traite pas les autres type d'évènement
 
      }
   }
}

Human.h
   
#ifndef HUMAN_H
#define HUMAN_H
 
#include <SFML\Graphics.hpp>
#include "Input.h"
 
class Human
{
public:
 
   Human::Human();
 
   // Accesseur
   int Human::getX(void) const;
   int Human::getY(void) const;
   int Human::getW(void) const;
   int Human::getH(void) const;
   float Human::getDirX(void) const;
   float Human::getDirY(void) const;
   float Human::getStartX(void) const;
   float Human::getStartY(void) const;
   int Human::getLife(void) const;
   int Human::getMunition(void) const;
   int Human::getRecharge(void) const;
 
   // Mutateur
   void Human::setX(int valeur);
   void Human::setY(int valeur);
   void Human::setW(int valeur);
   void Human::setH(int valeur);
   void Human::setDirX(float valeur);
   void Human::setDirY(float valeur);
   void Human::setStartX(float valeur);
   void Human::setStartY(float valeur);
   void Human::setLife(int valeur);
   void Human::setMunition(int valeur);
   void Human::setRecharge(int valeur);
 
   //Fonction
   void Human::draw(sf::RenderWindow &window);
   void Human::init(sf::RenderWindow &window);
   void Human::update(Input &input);
 
private:
 
   sf::Texture humanTexture;
   sf::Sprite humanSprite;
 
   const int speedPlayer = 5;
   int x, y;
   int w,h;
   float dirX, dirY;
   float startX, startY;
   int munition;
   int recharge;
   int life;
   int etat, direction;
 
   enum { IDLE,LEFT, RIGHT, UP, DOWN, SHOOT };
};
 
 
 
#endif // !HUMAN_H

Human.cpp
   
#include <SFML\Graphics.hpp>
#include <iostream>
#include "Human.h"
#include "Input.h"
 
Input input;
 
Human::Human()
{
   if (!humanTexture.loadFromFile("player/human-bas.png"))
   {
      std::cout << "Erreur lors du chargement de HUMAN" << std::endl;
   }
   else
   {
      humanSprite.setTexture(humanTexture);
   }
   x = y = 0;
   w = h = 32;
   dirX = 0;
   dirY = 0;
   startX = 100;
   startY = 200;
   munition = 32;
   recharge = 3;
    life = 100;
 
}
 
void Human::init(sf::RenderWindow &window)
{
   x = y = 0;
   w = h = 32;
   dirX = 0;
   dirY = 0;
   startX = 100;
   startY = 200;
   munition = 32;
   recharge = 3;
   life = 100;
}
// Accesseur
int Human::getX(void) const{ return x; }
int Human::getY(void) const{ return y; }
float Human::getDirX(void) const{ return dirX; }
float Human::getDirY(void) const{ return dirY; }
float Human::getStartX(void) const  { return startX; }
float Human::getStartY(void) const  { return startY; }
int Human::getLife(void) const{ return life; }
int Human::getMunition(void) const{ return munition; }
int Human::getRecharge(void) const{ return recharge; }
 
// Mutateur
void Human::setX(int valeur){ x = valeur; }
void Human::setY(int valeur){ y = valeur; }
void Human::setDirX(float valeur){ dirX = valeur; }
void Human::setDirY(float valeur){ dirY = valeur; }
void Human::setStartX(float valeur) { startX = valeur; }
void Human::setStartY(float valeur) { startY = valeur; }
void Human::setLife(int valeur){ life = valeur; }
void Human::setMunition(int valeur){ munition = valeur; }
void Human::setRecharge(int valeur){ recharge = valeur; }
 
void Human::draw(sf::RenderWindow &window)
{
   /*humanSprite.setPosition(sf::Vector2f(startX - w, startY - h));*/
   window.draw(humanSprite);
}
void Human::update(Input &input)
{
   if (input.getButton().UP == true)
   {
      std::cout << "UP" << std::endl;
      y -= speedPlayer;
      direction = UP;
   }
   else if (input.getButton().DOWN == true)
   {
      std::cout << "DOWN" << std::endl;
      y += speedPlayer;
      direction = DOWN;
   }
   else if (input.getButton().RIGHT == true)
   {
      std::cout << "RIGHT" << std::endl;
      x += speedPlayer;
      direction = RIGHT;
   }
   else if (input.getButton().LEFT == true)
   {
      std::cout << "LEFT" << std::endl;
      x -= speedPlayer;
      direction = LEFT;
   }
}

Merci pour votre aide :D
« Modifié: Juillet 23, 2015, 09:59:07 am par TheYoungGeek43 »
L'échec est la preuve que l'on à essayer

Jamese

  • Jr. Member
  • **
  • Messages: 72
    • Voir le profil
Re : SFML bug de deplacement
« Réponse #1 le: Juillet 23, 2015, 08:19:28 am »
personnellement pour faire bouger mes entité j'utilise sa :
   
 s_player.move(0,-speed);
ou
   
s_playeur.setPosition(xxx,yyy)


après si cela ne règle pas t'on problème je risque d'avoir du mal a t'aider.
« Modifié: Juillet 23, 2015, 08:22:17 am par Jamese »

TheYoungGeek43

  • Jr. Member
  • **
  • Messages: 87
    • Voir le profil
Re : SFML bug de deplacement
« Réponse #2 le: Juillet 23, 2015, 09:58:36 am »
J'ai trouver j'ai juste oublier de mettre un getPosition
L'échec est la preuve que l'on à essayer

Jamese

  • Jr. Member
  • **
  • Messages: 72
    • Voir le profil
Re : SFML bug de deplacement[resolus]
« Réponse #3 le: Juillet 23, 2015, 06:17:26 pm »
La réponse est souvent sous nos yeux et nous la voyons pas.

    Bonne continuation pour t'on projet.

TheYoungGeek43

  • Jr. Member
  • **
  • Messages: 87
    • Voir le profil
Re : SFML bug de deplacement[resolus]
« Réponse #4 le: Juillet 23, 2015, 10:40:43 pm »
Merci
L'échec est la preuve que l'on à essayer