Myjestic Posted November 10, 2017 Share Posted November 10, 2017 Hi all, how can I apply some registerBeforeRender actions to a childmesh within a for loop? Please look at this PG - Row 91 and 92. Finally, all cubes should spin. https://playground.babylonjs.com/#M4FKI9#1 I tried with arrays and objects for "Sphere" but this did not work. Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted November 10, 2017 Share Posted November 10, 2017 Hi @Myjestic Just push a reference of each clone into an array and loop the array https://playground.babylonjs.com/#M4FKI9#2 Quote Link to comment Share on other sites More sharing options...
Myjestic Posted November 10, 2017 Author Share Posted November 10, 2017 @aWeirdo ... sometimes ... I can't see the wood of the trees. Thank you, this is a good approach. But why is it not possible to do the registerBeforeRender in the same loop? Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted November 10, 2017 Share Posted November 10, 2017 @Myjestic It is also possible with a little change, adding the registerBeforeRender to the child itself, https://playground.babylonjs.com/#M4FKI9#3 I just prefer it the other way Have a nice friday. Myjestic 1 Quote Link to comment Share on other sites More sharing options...
RaananW Posted November 10, 2017 Share Posted November 10, 2017 Just for understanding - What you did it working. But only for the last sphere defined The async function you register (in the registerBeforeRender) is using the sphere variable reference. As sphere is defined in the scope above it, and javascript hoists variables, the sphere reference inside this function always directs to the last sphere you created. @aWeirdo (Hey, I always find it hard calling you that ) suggested a great way of solving this. Personally, I like using the forEach function over arrays, as it creates a new context each time you run it. If you want to register a function for each sphere, this is how I would to this: https://playground.babylonjs.com/#M4FKI9#4 I find it cleaner, but it is all a matter of taste in that case. Myjestic 1 Quote Link to comment Share on other sites More sharing options...
Myjestic Posted November 10, 2017 Author Share Posted November 10, 2017 You guys are great! Thank you. 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.