如何使用 Phaser.io 在 Tween 中创建弹跳或跳跃动作?

ces*_*sor 2 animation tween phaser-framework

我有以下代码:

var lion = game.add.sprite(2, 2,'lion');
var jump = game.add.tween(lion);
jump.to({x: 1000, y: 1000 }, 10000, Phaser.Easing.Linear.None);
// ... 
jump.start();
Run Code Online (Sandbox Code Playgroud)

我创建了一个精灵,想让它在两点之间移动,这里我将狮子从左上角移动到右下角的某个点(1000,1000)。是否可以在这个动作中添加弹跳动作?

目前狮子正在直线移动,但我想让它看起来好像狮子在跳跃,如下所示:

跳跃的狮子

我将如何实现这一目标?补间真的能够生成这样的复杂路径吗?

ces*_*sor 5

尽管该 API 很棘手并且有大量文档记录(恕我直言),但我还是设法找到了实现此行为的好点。我花了 2 天的时间来了解补间的工作原理以及在哪里应用我的更改。

创建补间时,您可以向其传递缓动函数。我想要的缓动仅适用于 Y 轴(弹跳运动),向右移动仅适用于 X 轴。因此我必须使用两个单独的补间:

function vanHalen (v) { // Might as well jump
    game.debug.spriteInfo(lion, 32, 32);
    return Math.sin(v * Math.PI) * 1; 
};

function goLion() {
    var move = game.add.tween(lion);
    var jump = game.add.tween(lion);

    // "Move" is a linear easing function that will move the sprite to (1000,y). It takes the Lion 2 Seconds to get there.
    move.to({x: 1000}, 2000);

    // The Jump is a function that works on the y axis, uses a customized easing function to "bounce". 

    jump.to({y: 30}, 500, vanHalen, true, 0, Number.MAX_VALUE, 0);
    move.start();
};
Run Code Online (Sandbox Code Playgroud)

跳跃会自动开始并且永远不会结束。当向右的运动结束后,狮子会继续在一处弹跳。

缓动函数接收一个进度值(介于 0 和 1 之间),该值指示补间已移动的距离(以百分比表示)。