如何为Tween.js分配额外的属性(https://github.com/sole/tween.js/)

时间:2013-10-27 20:53:15

标签: javascript animation tween tween.js

所以我正在研究两个值之间的补间,但不知道它们是位置还是缩放。因此我创建了一个Tween,它在更新循环/

上包含一个if语句
if( params.type == 'position' ){

  initial = {
    x: params.object.position.x,    
    y: params.object.position.y,
    z: params.object.position.z
  }

  target = {
    x: params.target.x,
    y: params.target.y,
    z: params.target.z,
  }

}else if( params.type == 'scale' ){

  intial = {
    x: params.object.scale.x,    
    y: params.object.scale.y,
    z: params.object.scale.z
  }

  target = {
    x: params.target.x,
    y: params.target.y,
    z: params.target.z,
  }


}

var tween = new TWEEN.Tween( initial ).to( target , params.time * 1000 );
tween.easing( params.easing );


// Need to assign these for the update loop
tween.object    = params.object;
tween.type      = params.type;
tween.tweener   = this;
tween.initial   = initial;
tween.target    = target;
tween.callback  = params.callback;

tween.onUpdate(function( tween ){

  if( this.type == 'position' ){
    this.object.position.x = this.initial.x;
    this.object.position.y = this.initial.y;
    this.object.position.z = this.initial.z;
  }else if( this.type == 'scale' ){
    this.object.scale.x = this.initial.x;
    this.object.scale.y = this.initial.y;
    this.object.scale.z = this.initial.z;
  }

  if( this.initial.x == this.target.x ){

    var i = this.tweener.tweens.indexOf( this );
    this.tweener.tweens.splice( i , 1 );

    this.callback();

  }

});

这个问题是在onUpdate循环中,

this

指的是

tween.intial

而不是

tween

有没有办法在这种情况下引用补间而不是我实际上在补间?

提前感谢您的帮助! 艾萨克

1 个答案:

答案 0 :(得分:0)

我最终在最后添加了一个绑定(我在javascript上的表现非常糟糕......)

tween.onUpdate(function(tween){

if(this.type =='position'){     this.object.position.x = this.initial.x;     this.object.position.y = this.initial.y;     this.object.position.z = this.initial.z;   } else if(this.type =='scale'){     this.object.scale.x = this.initial.x;     this.object.scale.y = this.initial.y;     this.object.scale.z = this.initial.z;   }

if(this.initial.x == this.target.x){

var i = this.tweener.tweens.indexOf( this );
this.tweener.tweens.splice( i , 1 );

this.callback();

}

}。bind(tween));

相关问题