如何在PhaserJS中添加一个tween,让对象从停止的地方一直往前移动?

问题描述 投票:0回答:1

需要的是运行对象并停止,现在进一步运行并停止,以此类推,根据函数的数字参数。repeat 在tween中,它从原来的起点开始重复。

我也试着用for循环给tweens添加迭代器。但它们在停止时没有给出停顿。我添加了 delaycompleteDelay 以获得暂停,但没有用。它只是直接运行到最终目的地。我甚至尝试了setTimeout,但没有用。

javascript ecmascript-6 phaser-framework tween
1个回答
0
投票

有一个方法我用来完成这个任务,它不需要 "repeat "参数,你可以在一个函数中添加你的tween,然后添加onComplete来调用这个tween的函数,例如。(使对象上升的tween)

function goAbove(yourTarget){
 this.tweens.add({
    targets: yourTarget,
    y: youTarget.y + newY,
    ease: "Linear",
    duration: duration,
    yoyo: false,
    onComplete: goAbove.bind(this, yourTarget),
  });
} 

除非你在tween的函数中添加一个if条件,让它在达到一个特定的值时停止,否则yourTarget会无休止地上升,例如:当yourTarget.y大于300时,上面这个条件会让tween停止重复。

if ( yourTarget.y < 300 ){
  this.tweens.add({
    targets: yourTarget,
    y: youTarget.y + newY,
    ease: "Linear",
    duration: duration,
    yoyo: false,
    onComplete: goAbove.bind(this, yourTarget),
  });
}

当yourTarget.y大于300的时候,上面的这个条件就会让tween停止重复,希望能帮到你。


0
投票

经过一段时间的搜索,我使用了Phaser liabrary中的tween timeline。它在for循环中也能完美地工作。@Adam Nijjar的解决方案也是可行的:)

© www.soinside.com 2019 - 2024. All rights reserved.