2017-01-05 7 views
0

스프라이트의 이동을위한 클래스를 애니메이션, 아주 기본적인 것들과 함께 만들었습니다.SFML 스프라이트 이동

마침내 모든 오류를 제거한 후 기본 논리를 모두 확인한 결과 운동 벡터는 내가 원한 것이고 모든 것이 괜찮아 보였다는 것을 알았습니다. 문제는 일단 스프라이트 이동을위한 버튼을 누르는 것입니다. 잠시 동안 움직 인 다음 원래 위치로 돌아갑니다.

내 클래스 이동 (스프라이트의 이동 및 애니메이션을 담당)은 이동 중에 설정된 변수에 따라 이동하는 주 소스 코드에서 사용되는 스프라이트를 반환합니다.

변수를 충분히 높은 값으로 설정하면 스프라이트가 원래 위치를 즉시 이동 한 다음 돌아와서 다시 이동하고 return.I 코드를 검사 한 것을 볼 수 있습니다. 스프라이트를 재설정하지 않았습니다. 위치 또는 그런 것. 다음은 코드입니다 : -

소스 코드 -

여기
#include"SFML\Graphics.hpp" 
#include"check.h" 
#include"display.h" 
int main() 
{ 
sf::Sprite plop; 
sf::RenderWindow window(sf::VideoMode(1360, 720), "Larger SFML", sf::Style::Default); 
sf::Texture texture; 
texture.loadFromFile("bahamut.png"); 
texture.setRepeated(false); 
float fraps = 0.0; 
check playa; 
sf::Clock fps; 
while (window.isOpen()) 
{ 
    fraps = fps.restart().asSeconds(); 
    plop = playa.movereturn(100000.,fraps,&texture); 
    window.clear(); 
    window.draw(plop); 
    display dis(window); 
} 

return 0; 

} 

체크의 헤더입니다 : -

여기
#pragma once 
#include"SFML\Graphics.hpp" 
class check 
{ 
public: 
    check(); 
    sf::Sprite movereturn(float speed,float fps,sf::Texture* texture); 
    ~check(); 
}; 

체크의 정의입니다 : -

#include "check.h" 



check::check() 
{ 
} 

sf::Sprite check::movereturn(float speed,float fps,sf::Texture* texture) 
{ 
    sf::Sprite playas; 
    playas.setTexture(*texture); 
    sf::Vector2f movements = { 0.,0. }; 
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) 
    { 
     movements = { -speed*fps,0. }; 
     playas.move(movements); 
    } 
    else 
    { 
     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) 
     { 
      movements = { speed*fps, 0. }; 
      playas.move(movements); 
     } 
     else 
     { 
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) 
      { 
       movements = { 0.,speed*fps }; 
       playas.move(movements); 
      } 
      else 
      { 

       if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) 
       { 
        movements = { 0.,-speed*fps }; 
        playas.move(movements); 
       } 
       else 
        movements = { 0., 0. }; 

      } 
     } 
    } 
    return playas; 

} 


check::~check() 
{ 
} 

Display는 윈도우를 가져 와서 윈도우에 window.display() 함수를 가지고 있습니다.이 클래스가 없으면 핸들러가 있습니다. 예외 그래서 나는 이것을 사용하도록 강요 당한다.

+0

당신은 단지 필요없이() window.display를 호출 할 수 있어야합니다 당신의 디스플레이 클래스를 사용하는 경우 기본적으로 동일한 코드이므로 다른 질문도 함께 업데이트해야합니다. – Eddge

답변

0

코드에 몇 가지 문제가 있습니다.

  1. 플로트에 올바른 구문을 사용해야합니다. 0.0f는 float, 0.0은 double을 나타냅니다. 놀라움은 컴파일러가 그것에 대해 경고하지 않습니다.
  2. 당신의 스프라이트가 당신의 movereturn 함수의 모든 프레임에 다시 만들어지고 있기 때문에, 당신의 스프라이트는 그에게 당신이 말하는 방향에 상관없이 움직일뿐입니다. 바로 다음 프레임으로 기본 값 (0의 위치)으로 다시 만들어집니다. 내장 된 Visual Studio 디버거를 사용한다면 스프라이트의 위치가 항상 기본으로 재설정 된 모든 프레임을 볼 수 있습니다. 너의 문제에 대한 큰 암시.
  3. 플레이어가 한 방향으로 만 이동할 수 있도록 제한하고 있으므로 대각선으로 이동할 수 없습니다 (위 왼쪽). else 문이 모두 필요하지 않습니다.
  4. 항상 1 줄 if 문에 항상 대괄호가 사용되는지 여부에 관계없이 대괄호를 사용하십시오. 당신은 그것으로 도망 갈 수 있지만, 괄호를 사용할 때 디버깅이 훨씬 간단 해집니다.

이 문제를 해결해야한다.

Check.h

#pragma once 
#include"SFML\Graphics.hpp" 
class check 
{ 
    sf::sprite playas; 
public: 
    check(); 
    sf::Sprite movereturn(float speed,float fps,sf::Texture* texture); 
    ~check(); 
}; 

확인합니다.CPP

sf::Sprite check::movereturn(float speed,float fps,sf::Texture* texture) 
{ 
    playas.setTexture(*texture); 
    sf::Vector2f movements = { 0.0f,0.0f }; 
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) 
    { 
     movements = { -speed*fps,0.0f }; 
    } 
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) 
    { 
     movements = { speed*fps, 0.0f };   
    } 
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) 
    { 
     movements = { 0.0f,speed*fps }; 
    } 
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) 
    { 
     movements = { 0.0f,-speed*fps }; 
    } 
    playas.move(movements); 
    return playas; 
} 

당신은 대각선 움직임 당신이 당신의 movereturn 기능이 귀하의 코드를 변경할 수 원한다면 :

sf::Sprite check::movereturn(float speed,float fps,sf::Texture* texture) 
{ 
    playas.setTexture(*texture); 
    sf::Vector2f movements = { 0.0f,0.0f }; 
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) 
    { 
     movements.x = -speed*fps; 
    } 
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) 
    { 
     movements.x = speed*fps;   
    } 
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) 
    { 
     movements.y = speed*fps; 
    } 
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) 
    { 
     movements.y = -speed*fps; 
    } 
    playas.move(movements); 
    return playas; 

}