gpt4 book ai didi

c++ - Sprite 运动

转载 作者:行者123 更新时间:2023-11-28 08:24:16 27 4
gpt4 key购买 nike

我正在为这个而撕扯我的头发。出于某种奇怪的原因,我找不到/想不出如何在 SFML 和/或 SDL 中移动 Sprite 。我看过的这两个库的教程对此一无所知;所以我认为它更像是 C++ 的东西而不是库的东西。

所以我想知道;你如何移动 Sprite ?

(当我说移动时,我的意思是让 Sprite 以设定的速度“滑过”窗口)

最佳答案

您需要一个每秒调用固定次数的循环,然后每帧更新 Sprite 的 x,y 值。

对于 SFML 你有 sprite.move

while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}

// Get elapsed time
float ElapsedTime = App.GetFrameTime();

// Move the sprite
if (App.GetInput().IsKeyDown(sf::Key::Left)) Sprite.Move(-100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Right)) Sprite.Move( 100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Up)) Sprite.Move(0, -100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Down)) Sprite.Move(0, 100 * ElapsedTime);
}

关于c++ - Sprite 运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4600130/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com