我看不到精灵的动画

时间:2019-07-28 23:45:48

标签: c++ sfml

这是包含以下内容的代码:

Sprite player;
Texture playerTexture;
IntRect playerContainer(0, 0, 32, 32);
Vector2f playerPosition;

int playerDirection = 0; // 0 - fwd, 1 - back, 2 - stay

这是动画更新方法:

void updateAnims(Clock clock, float time) {
  if(time > 0.3f) {
    if(playerDirection == 0) playerContainer.top = 0;
    else if(playerDirection == 1) playerContainer.top = 32;
    else if(playerDirection == 2) playerContainer.top = 64;

    if(playerContainer.left == 96) playerContainer.left = 0;
    else playerContainer.left += 32;

    player.setTextureRect(playerContainer);
    clock.restart();
  }
}

,并且此方法在“ int main()”方法中进行了更新。

int main() {
  // Init window
  RenderWindow window(VideoMode(800, 600), "RPG");
  Clock gameClock;
  Clock animClock;
  float gameTime;
  float animTime;

  // Setting up the player
  playerTexture.loadFromFile("player.png");
  player.setTexture(playerTexture);
  player.setTextureRect(playerContainer);
  player.setScale(Vector2f(3.f, 3.f));
  playerPosition.x = 30;
  playerPosition.y = 120;
  player.setPosition(playerPosition);

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

    checkInputs(gameTime);

    animTime = animClock.getElapsedTime().asSeconds();
    updateAnims(animClock, animTime);

    window.clear();

    gameTime = gameClock.getElapsedTime().asMilliseconds();
    gameClock.restart();

    window.display();
  }

  return 0;
}

结果表明,精灵是在main方法中创建的,获取纹理和纹理形状,获取位置,但未绘制。为什么?

我认为问题出在动画方法上,但是我尝试了不同的解决方案。

1 个答案:

答案 0 :(得分:0)

您应该在count(distinct id)window.draw(player)之间调用window.clear()方法,以在屏幕上实际绘制玩家精灵。