无键盘输入的SFML动画
SFML animation without keyboard input
我目前正在进行一个项目,该项目基本上是排序算法的可视化,以解释它们的工作原理(而不是概述)。我是使用SFML(甚至OpenGL)的新手,对该库的经验有限,但我想做的是将绘制的精灵移动到不同的位置以显示排序。我看过教程和示例,但它们都采用了键盘输入来移动精灵——这是本项目中没有使用的。有人知道如何做到这一点吗?
这是当前代码:
DrawCups.h
class DrawCups
{
public:
DrawCups(sf::RenderWindow& window);
~DrawCups();
void loadImage(const char* pathname, sf::Texture& texture, sf::Sprite& sprite);
void drawCup1();
private:
sf::RenderWindow& _window;
};
DrawCups.pp(选定功能)
void DrawCups::drawCup1()
{
// load our image
sf::Texture cup1; // the texture which will contain our pixel data
sf::Sprite cup1Sprite; // the sprite which will actually draw it
loadImage("./images/InsertionSort/red_cup_1.png", cup1, cup1Sprite);
cup1Sprite.setPosition(sf::Vector2f(150, 230));
_window.draw(cup1Sprite);
}
main.cpp
int main()
{
sf::RenderWindow window(sf::VideoMode(1366, 768), "Sorting Algorithm Visualisation: SFML");
window.setFramerateLimit(60);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::White);
DrawCups drawToWindow(window);;
drawToWindow.drawCup1();
window.display();
}
return 0;
}
在循环之前创建图像,并在绘制之前进行更新。
DrawCups drawToWindow(window); //Constructor creates the sprite
while (window.isOpen())
{
...
drawToWindow.update(); //Update the position
//Redraw
window.clear(sf::Color::White);
drawToWindow.drawCup1();
window.display();
}
我不确定你想要什么类型的移动,但更新功能可以是这样的:
void DrawCups::update()
{
sf::Vector2f pos = this->cup1Sprite.getPosition();
pos.x++; //Move 1 pixel to the left
this->cup1Sprite.setPosition(pos);
}
显然,要根据自己的需要改变动作。如果移动过快或过慢,请进行较小/较大的更新。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- SFML纹理像播放器
- 如何使用AngelScript注册SFML Vector2运算符
- 如何修复sfml c++代码编译错误
- SFML library: http request
- 落砂模拟碰撞检测C++和SFML
- 使用 SFML 和 C++ 将 Pixel 打印到屏幕上
- SFML RenderWindow打开窗口需要很长时间
- 使用 GLUT 使用键停止动画?
- 矢量下标超出SFML游戏中的范围
- 获取 SFML 窗口的 HWND 和高可用性?
- 将 Vulkan 与 SFML 结合使用?
- SFML 文本未绘制在窗口上
- SFML 碰撞永远不会在我的系统中注册
- 使用对象数组对 SFML 进行动画处理
- (SFML)按下键时,播放器构造函数未使用正确的动画进行更新
- 从精灵表在 sfml 中为精灵制作动画
- 制作平滑滑行动画 SFML
- 鼠标点击后爆炸的sfml动画
- 无键盘输入的SFML动画