使用 libgdx 创建交互式动画
creating interactive animation with libgdx
我正在寻找一种将平移手势与动画完成百分比联系起来的方法。让我告诉你我的意思。
此图像表示我要执行的动画,即移动图像演员或精灵。动画通过 pan 手势执行。当用户滑动 200 像素时,动画已 100% 完成并处于第 6 阶段。如果用户仅滑动 100px,它将完成 50% 并处于第 3 阶段。如果用户没有执行平移手势,动画将保持在 0% 和第 1 阶段。我正在寻找有关如何开始构建此类模型的提示.我相信这叫做互动。你有什么建议吗?
您可以使用 GestureDetector 来处理平移输入。 gestureListener.pan
方法可以更新动画位置参数。
private int screenPixelsToAnimationPositionRatio = 0.01f; //will need to tweak this and probably adjust it at runtime based on screen dimensions and resolution
private float animationPosition = 0; //from 0 to 1, fraction of animation complete
public void create () {
//...
GestureAdapter gestureAdapter = new GestureAdapter {
@Override
public boolean pan (float x, float y, float deltaX, float deltaY) {
animationPosition += deltaX * screenPixelsToAnimationPositionRatio;
animationPosition = MathUtils.clamp(animationPosition, 0, 1);
return true;
}
};
GestureDetector gestureDetector = new GestureDetector(gestureAdapter);
Gdx.input.setInputProcessor(gestureDetector); //or put the detector in an InputMultiplexer with your other input listeners.
}
然后您将创建一个方法,可以根据 animationPosition
的当前值更新对象的位置和旋转。您需要找出确定所需运动的方程式。例如,看起来有点像您在上面说明的内容:
private void updateAnimation (){
x = animationPosition * 30;
float y = 0, rotation = 0;
if (animationPosition >= 0.25f) {
float jumpPosition = Math.min(1, (animationPosition - 0.25f) / 0.5f);
y = 30 * (1 - Interpolation.pow2In.apply(Math.abs(2 * jumpPosition - 1)));
rotation = 180 * jumpPosition;
}
mySprite.setPosition(x, y);
mySprite.setRotation(rotation);
}
然后在render
的某处调用这个更新方法。
我正在寻找一种将平移手势与动画完成百分比联系起来的方法。让我告诉你我的意思。
此图像表示我要执行的动画,即移动图像演员或精灵。动画通过 pan 手势执行。当用户滑动 200 像素时,动画已 100% 完成并处于第 6 阶段。如果用户仅滑动 100px,它将完成 50% 并处于第 3 阶段。如果用户没有执行平移手势,动画将保持在 0% 和第 1 阶段。我正在寻找有关如何开始构建此类模型的提示.我相信这叫做互动。你有什么建议吗?
您可以使用 GestureDetector 来处理平移输入。 gestureListener.pan
方法可以更新动画位置参数。
private int screenPixelsToAnimationPositionRatio = 0.01f; //will need to tweak this and probably adjust it at runtime based on screen dimensions and resolution
private float animationPosition = 0; //from 0 to 1, fraction of animation complete
public void create () {
//...
GestureAdapter gestureAdapter = new GestureAdapter {
@Override
public boolean pan (float x, float y, float deltaX, float deltaY) {
animationPosition += deltaX * screenPixelsToAnimationPositionRatio;
animationPosition = MathUtils.clamp(animationPosition, 0, 1);
return true;
}
};
GestureDetector gestureDetector = new GestureDetector(gestureAdapter);
Gdx.input.setInputProcessor(gestureDetector); //or put the detector in an InputMultiplexer with your other input listeners.
}
然后您将创建一个方法,可以根据 animationPosition
的当前值更新对象的位置和旋转。您需要找出确定所需运动的方程式。例如,看起来有点像您在上面说明的内容:
private void updateAnimation (){
x = animationPosition * 30;
float y = 0, rotation = 0;
if (animationPosition >= 0.25f) {
float jumpPosition = Math.min(1, (animationPosition - 0.25f) / 0.5f);
y = 30 * (1 - Interpolation.pow2In.apply(Math.abs(2 * jumpPosition - 1)));
rotation = 180 * jumpPosition;
}
mySprite.setPosition(x, y);
mySprite.setRotation(rotation);
}
然后在render
的某处调用这个更新方法。