首页 > 解决方案 > 无论如何使用矩形在 sfml 中创建动画

问题描述

有什么方法可以在 sfml 中创建动画。不是精灵表。但有 1 个矩形到另一个位置。 https://gyazo.com/0952a960bd7f9e5c9c5e1fe71d0f0b45 我想要类似的打孔效果。但我不知道从哪里开始。

有没有办法在 sfml 中制作流畅的动画?

标签: c++algorithmanimationsfml

解决方案


最优雅的方法是使用精灵表,但也可以不用。制作一个移动矩形位置并将其返回的 for 循环

float smoothness //try for your values
float directionX; //if negative punch left set values 1 or -1
float directionY; //if negative punch down set values 1 or -1
int reach;
for(int i=0;i<reach;i++)
{
     rect.setPosition(
     directionX*(rect.getPosition.x+smoothness)
     ,directionY*(rect.getPosition.y+smoothness))
}
//return to original position
for(int i=0;i<reach;i++)
{
     rect.setPosition(
     -directionX*(rect.getPosition.x+smoothness)
     ,-directionY*(rect.getPosition.y+smoothness))
}

注意: 请设置一个恒定的帧速率以使其正常工作


推荐阅读