trecool Posted August 8, 2016 Share Posted August 8, 2016 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! Quote Link to comment Share on other sites More sharing options...
adam Posted August 8, 2016 Share Posted August 8, 2016 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)); GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.