JS:如何执行从函数2调用函数2的函数1?

时间:2021-12-28 18:12:30

I'm making an animation system for a canvas game. Every time I want to move a player, I use entities[0].moveTo(...), entities[0] is an object, moveTo() is its method that executes larger animate() function.

我正在为帆布游戏制作动画系统。每次我想移动一个播放器时,我都使用实体[0] .moveTo(...),实体[0]是一个对象,moveTo()是执行更大​​的animate()函数的方法。

var moveTo = function(x,y,d){
    if(x!=0) animate(this,'x',this.coords.x+x*tilescale,d,animation_mode);
    if(y!=0) animate(this,'y',this.coords.y+y*tilescale,d,animation_mode);
}
var animate = function(e, prop, val, duration, mode) {
    if(mode){
        var start = new Date().getTime();
        var end = start + duration;
        var current = e["offset"][prop];
        var distance = val - current;
        var step = function() {
            var timestamp = new Date().getTime();
            var progress = Math.min((duration - (end - timestamp)) / duration, 1);
            e["offset"][prop] = current + (distance * progress);
            render();
            if (progress < 1) requestAnimationFrame(step);
            else ## moveTo.onanimationfinished() ##
        };

        return step();
    }
    else{
        return;
    }
};

Basically I want a "bumping" animation from two small transforms -- go forward, then go back quick. "go back" part should be shown when "go forward" part is complete. Any way to do this?

基本上我想从两个小变换中获得“碰撞”动画 - 前进,然后快速返回。当“前进”部分完成时,应该显示“返回”部分。有什么办法吗?

1 个解决方案

#1


0  

You can create a new function bumb and then call it in the end of animate, but you can also give another argument in animate function called bumb like this:

您可以创建一个新的函数bumb,然后在animate结束时调用它,但是您也可以在animate函数中给出另一个参数bumb,如下所示:

var animate = function(e, prop, val, duration, mode, bump) {
   if(bump){
      // Bump animatio
   }
   else{
      // Move animation
   }
}

#1


0  

You can create a new function bumb and then call it in the end of animate, but you can also give another argument in animate function called bumb like this:

您可以创建一个新的函数bumb,然后在animate结束时调用它,但是您也可以在animate函数中给出另一个参数bumb,如下所示:

var animate = function(e, prop, val, duration, mode, bump) {
   if(bump){
      // Bump animatio
   }
   else{
      // Move animation
   }
}