Render loop - class method?

Hi

Refactoring my code into class, but I have one last problem
I’d like to remove the subfunction and transform it into a class method like anything else
but i cant make the render loop work (js error on “this” )

    // current
    class RenderEngine {
        init{
            //        ......  create all scene, camera, mesh ..

          
           render();
           function render() {
            stats.update();
            requestAnimationFrame(render);
            renderer.render(scene, camera);

        }
   }

Goal :

    // current
    class RenderEngine
    {
        init{
            //        ......  ...  this.camera = ; ..

             this.render();  // <------------------

       } 

        // an individual class method
         render() {
                this.stats.update();
                this.requestAnimationFrame(this.render);
                this renderer.render(this.scene, this.camera);
         }
     }

I can’t make it work :frowning:
Has anyone a tips ? ( neither familiar of js function into function nor like it… : “Keep it simple”)

Thks !

I’m afraid you have not posted valid JavaScript Syntax.

class RenderEngine {
    init {
    }
}

Something like this will produce syntax errors.

Why not provide a fiddle with a simple working example. In this way, you can easier show what parts of the code you want to change.

good idea :slight_smile:

The working simple code
https://jsfiddle.net/35vrenyL/

"what i’d like to do " (kick out the nested function)
https://jsfiddle.net/cbt2m5tm/1/

Have a look at: https://jsfiddle.net/cbt2m5tm/3/

Background information:

2 Likes

Thanks a lot :+1::+1: