"this" is not recognized in mesh.actionManger.registerAction

#1

Hi everybody,
(Sorry for my English, I hope you will understand me)
I am starting on babylonJS and JavaScript but I have succeeded to create a kind of “virtual visit”. But now after that code, I wanted to structure it. Here is my “virtual visit” : https://www.babylonjs-playground.com/#FFACFW#16
Here my beginning of code “structured” : https://www.babylonjs-playground.com/#8987DV#10

Here my problem: I want to create my triggers now with the actionManager ( I succeeded with my first code without classes). So I have created my class Trigger. Untill here, all is ok, my spheres are displayed etc… To create different animation ( new material on a mesh etc…) I created a class Animation where I keep the code for changing the material etc…

here my classes Trigger & Animation :

So my problem is that this.animation is not recognized in the function of mesh.actionManager
but this.animation is recognized in the function addPointerOverTrigger before the mesh.actionManager.
When I run it with this.animation in the function of mesh.actionManager :
“this.animation is undefined”
But when I run it without that and this.animation before mesh.actionManager so all is Ok but it’s not what I want…

I have tried to give a parameter to the function of mesh.actionManager : function(this.animation) but It’s not working.
I have tried also to give a parameter to the trigger with mesh.actionManager.registerAction(new BABYLON.ExecuteCodeAction({ trigger: BABYLON.ActionManager.OnPointerOverTrigger, parameter: this.animation} , function () })); but it’s not working too…

I don’t understand why all function of babylonJS can be called in the manager but not this.animation.

Please can you help me?
Hope you will find a solution.

Best regards.

#2

Hi Mathieu,

I’m not sure if I’m understanding it right, are you saying that in your method ‘addPointerOverTrigger(mesh)’, the this.animation.changeMaterial(mesh, greenMat) line does work, but the content inside the actionManager inside that same method is not working/ not recognising this.animation.changeMaterial(mesh, greenMat)?

#3

Hi, when you do

console.log(this);

You will see that this is now referencing the ExecuteCodeAction, that’s where the function resides. To fix it, you need to capture the local scope:

Instead of:

function(){
}

Use this:

() => {
}
1 Like
#4

It’s working, thank you both for your time ! And yes it was that grolivas :slight_smile:
Thank for teaching me this concept

1 Like