I’m using the Tween library to reposition a model in various locations. When I move the model, I want to rotate it around a different apparent pivot point. To do that, I use a parent entity to do the rotating. When I want to change the pivot location, I move the model relative to the parent and then move the parent by an exact offsetting amount. This occurs in one frame (dt) so that the model does not appear to move at all. It works nicely. But here’s the problem. I want to run this bit of code upon completion of a tween function. But when I move the code to a function called by the tween complete, I get the error:
Uncaught TypeError: Cannot read property ‘getPosition’ of undefined
I’ve figured out that at least one part of the problem is that when I use “this” inside the tween “complete” function that “this” now refers to the tween function and not the script that I’m working on.
My question is how do I properly reference these entities when calling them from the tween’s complete function?
Thanks for any insight that can be offered.
Btn_ERFSII_Vent.prototype.onPress = function (event) {
event.element.materialAsset = this.originalMaterial;
this.buttonPressed = true;
this.counter = 0;
this.Btn_ERFSII_Vent.element.useInput = false;
this.Btn_ERFSII_Hoses.element.useInput = false;
this.Btn_ERFSII_Restraint.element.useInput = false;
this.Btn_ERFSII_Bladder.element.useInput = false;
this.Btn_ERFSII_Electrical.element.useInput = false;
this.Btn_ERFSII_BkwyValves.element.useInput = false;
this.Btn_ERFSII_CtlPanel.element.useInput = false;
this.tweenSpinReset = this.tankPivotNULL.tween(this.tankPivotNULL.getLocalEulerAngles()).rotate({x: 0, y: 0, z: 0 }, 1, pc.SineInOut)
.on('complete', function (){
console.log('tween completed');
console.log(this);
//Code fails here
pivotPos = this.tankPivotNULL.getPosition();
targetPos = this.pivotTarget.getPosition();
initPos = this.ERFS_Tank1.getPosition();
shiftAmt = pivotPos.sub(targetPos);
this.tankPivotNULL.setPosition(targetPos);
initPos = this.ERFS_Tank1.getPosition();
this.ERFS_Tank1.setPosition(initPos.add(shiftAmt));
});
this.tweenSpinReset.start();
this.tweenSpin = this.Tank1MasterNULL.tween(this.Tank1MasterNULL.getLocalEulerAngles()).rotate({x: -20, y: -0, z: -20}, 2.0, pc.SineInOut).delay(1);
this.tweenSpin.start();
this.tween = this.Tank1MasterNULL.tween(this.Tank1MasterNULL.getLocalPosition()).to({x: 6.026, y: 1.944, z: -5.665}, 2.0, pc.SineInOut).delay(1);
this.tween.start();
// Code works fine in this location
/*pivotPos = this.tankPivotNULL.getPosition();
targetPos = this.pivotTarget.getPosition();
initPos = this.ERFS_Tank1.getPosition();
shiftAmt = pivotPos.sub(targetPos);
this.tankPivotNULL.setPosition(targetPos);
initPos = this.ERFS_Tank1.getPosition();
this.ERFS_Tank1.setPosition(initPos.add(shiftAmt));*/
};