Jump to content

Calling functions with ExecuteCodeAction / Accessing an array


trecool
 Share

Recommended Posts

Hey! 

So I'm trying to re-use a function because it can easily be done but it doesn't seem to work for me. I've got an array filled with cubes and I'm trying to call a function with an id as parameter to reference the objects within the array.

 

cubes.push(cube, cube1, cube2, cube3);

            function lightFader (id) {
                if (!clicked) {
                    cubes[id].material.diffuseColor = new BABYLON.Color3(1.00, 0.00, 0.00);
                    document.getElementById("p1").innerHTML = "Text 1!";
                    clicked = true;                    
                } else {
                    cubes[id].material.diffuseColor = new BABYLON.Color3(1.00, 1.00, 0.00);
                    document.getElementById("p1").innerHTML = "Text 2!";
                    clicked = false;
                }
            }

            cube.actionManager = new BABYLON.ActionManager(scene);
            cube1.actionManager = new BABYLON.ActionManager(scene);
            cube2.actionManager = new BABYLON.ActionManager(scene);
            cube3.actionManager = new BABYLON.ActionManager(scene);

            cube.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickTrigger, lightFader(0)));
            cube1.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickTrigger, lightFader(1)));
            cube2.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickTrigger, lightFader(2)));
            cube3.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickTrigger, lightFader(3)));

 

I have two major issues with this code:

Firstly, I know that functions with parentheses are called on start and functions without them are called, when they are referenced in code. Meaning lightFader; only gets called on the trigger event while lightFader() gets called as soon as I launch the whole thing. But I don't want to do that, I do not want it to run on start up. How do I do that if I do require the parentheses to call the function with the right parameter?

Secondly, it does not seem to be possible to change the material using my approach. What is wrong here? Is this approach completely wrong?

Thank you! 

Link to comment
Share on other sites

cube.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickTrigger, funtion(){lightFader(0);}));

 

or

 

function lightFader (evt) {
    var mesh = evt.source;
    if (!clicked) {
        mesh.material.diffuseColor = new BABYLON.Color3(1.00, 0.00, 0.00);
        document.getElementById("p1").innerHTML = "Text 1!";
        clicked = true;                    
    } else {
        mesh.material.diffuseColor = new BABYLON.Color3(1.00, 1.00, 0.00);
        document.getElementById("p1").innerHTML = "Text 2!";
        clicked = false;
    }
}

cube.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickTrigger, lightFader));

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...