So I am working on tweening between two values, but do not know if they will be position or scale. Because of this I am creating a Tween which contains an if statement on the update loop/

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();

  }

});

The problem with this is that in the onUpdate loop, the

this

refers to

tween.intial

instead of

tween

Is there any way to reference tween in this situation rather than what I am actually tweening?

Thank you in advance for your help! Isaac

有帮助吗?

解决方案

I ended up adding a bind to the end( I'm very bad at javascript it turns out... )

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 ));

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top