2012-06-09 4 views
0

두 클래스를 썼습니다. 애니메이션 및 배우. 어떤 이유로 스프라이트 이미지가 변경되지 않습니다. 그것은 전체 시간 동안 첫 번째 프레임을 유지합니다. 코드는 괜찮습니다. 컴파일 할 때 문제가 없습니다. 논리가 어딘가 잘못되어 예상대로 작동하지 않습니다.스프라이트가 움직이지 않음

애니메이션 클래스 선언 :

class Animation 
{ 
public: 
     Animation(std::string path, int frames); 
     ~Animation(); 
     void nextFrame(); 
     void gotoStart(); 
     bool loadFrames(); 
     sf::Texture& getActiveFrame(); 

private: 
     int frameCount; 
     std::string pathToAnimation; 
     int currentFrame; 
     sf::Texture frame[]; 
}; 

애니메이션 클래스 구현 :

Animation::Animation(std::string path, int frames) 
{ 
     pathToAnimation = path; 
     frameCount = frames; 
} 

Animation::~Animation() 
{ 
     // destructor 
} 

void Animation::nextFrame() 
{ 
     if(currentFrame < frameCount) 
     { 
      currentFrame = 1; 
     } 
     else 
      currentFrame += 1; 
} 

void Animation::gotoStart() 
{ 
     currentFrame = 1; 
} 

bool Animation::loadFrames() 
{ 
     for(int i = 01; i < frameCount; i++) 
     { 
      if(!frame[i].loadFromFile(pathToAnimation + std::to_string(i) + ".jpg")) return false; 
     } 
     return true; 
} 

sf::Texture& Animation::getActiveFrame() 
{ 
    return frame[currentFrame]; 
} 

Actor 클래스 선언 :

class Actor 
{ 
public: 
     Actor(); 
     ~Actor(); 
     void setActiveAnimation(std::shared_ptr<MaJR::Animation> anim); 
     void draw(sf::RenderWindow& win); 

private: 
     sf::Sprite sprite; 
     std::shared_ptr<MaJR::Animation> activeAnimation; 
}; 

배우 클래스 구현 :

Actor::Actor() 
{ 
    // constructor 
} 

Actor::~Actor() 
{ 
    // destructor 
} 

void Actor::setActiveAnimation(std::shared_ptr<MaJR::Animation> anim) 
{ 
    activeAnimation = anim; 
    activeAnimation->gotoStart(); 
} 

void Actor::draw(sf::RenderWindow& win) 
{ 
    sprite.setTexture(activeAnimation->getActiveFrame()); 
    win.draw(sprite); 
    activeAnimation->nextFrame(); 
} 

여기를 테스트하는 코드입니다 :

int main() 
{ 
    sf::RenderWindow Window(sf::VideoMode(800, 600), "MaJR Game Engine Sandbox"); 

    std::shared_ptr<MaJR::Animation> DroneStandNorth = std::make_shared<MaJR::Animation>("./Assets/Sprites/Zerg/Units/Drone/Stand/North/", 61); 
    if(!DroneStandNorth->loadFrames()) return EXIT_FAILURE; 
    MaJR::Actor Drone; 
    Drone.setActiveAnimation(DroneStandNorth); 

    while(Window.isOpen()) 
    { 
     sf::Event Event; 
     while(Window.pollEvent(Event)) 
     { 
      if(Event.type == sf::Event::Closed) 
       Window.close(); 
     } 

     if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) 
      Window.close(); 

     Window.clear(); 
     Drone.draw(Window); 
     Window.display(); 
    } 

    return 0; 
} 

내가 여기에 잘못 무엇인지에 완전한 손실에 있어요. 모든 것을 스스로 컴파일하려면 여기에 원본 파일 http://www.filedropper.com/animationtesttar을 사용하십시오. 컴파일러에서 C++ 11을 사용하려면 -std = C++ 0x 또는 사용자가해야하는 일을 수행하십시오. 애니메이션 구현에서

답변

1

, 당신이 :

void Animation::nextFrame() 
{ 
    if(currentFrame < frameCount) 
    { 
     currentFrame = 1; 
    } 
    else 
     currentFrame += 1; 
} 

currentFrame는 1에서 시작하기 때문에, 항상 frameCount 미만, 그래서 항상 1. 변경으로 설정됩니다

void Animation::nextFrame() 
{ 
    if(currentFrame >= frameCount) 
    { 
     currentFrame = 1; 
    } 
    else 
     currentFrame += 1; 
} 

이렇게하면 currentFrame이 (테스트 케이스 61에서) frameCount와 같으면 재설정됩니다. Actor()의 Draw() 다음에 nextFrame()이 호출되기 때문에 마지막 프레임이 그려지며 currentFrame은 다시 1로 재설정됩니다.

관련 문제