Jump to content

Accesing the scene variable from another class


Recommended Posts

I am teaching myself how to use babylon.js. Me and my friend are creating an animation class where all of our animations will be handled. In our main manager class he declared the variable scene as a private property, but, he created a method called getScene where he returns scene. In my animations class I have to write the line scene.beginAnimation(...) ; but my question is: how do I access the scene if its being instantiated somewhere else? 

Link to comment
Share on other sites

Hi MM!  I'm no expert on classes, but take a look at a basic scene...


Take a look at line 7.

The params/args to instantiate a BABYLON.FreeCamera... contains the scene.  It is the last parameter of the constructor.

Let's call your class... AnimationManager.  When you want to instantiate an AnimationManager, why not do the same thing.

var myAniMan = new AnimationManager("am1", scene);

Let's look at the BABYLON.FreeCamera class...


See line 11?  Scene is the last param/arg.  In your class... it might look like...

function AnimationManager(name, scene) {

Instantly, you have access to scene. No need for a getScene() function in your class.

Generally, I think it is wise to make your classes look and act like the BABYLON classes.  And, ideally, write them in TypeScript.  You can use the online TypeScript compiler to grind them into JS for testing.  But you can convert to TypeScript later, too.

And it might be wise to make the purpose of your AnimationManager... be to manage BABYLON Animation-class objects:)  It starts them, it stops them, it adjusts them, it clones them, it moves them from one mesh.animations array to another mesh.animations array.  (cameras and lights, too!)  The framework's Animation objects are excellent.  And we have some brand new easing functions thanks to a chap named Mimetis.

And, I think you'll find that our ActionManager has some real sweet user interaction powers and animation interpolators, as well.  It can also be configured to start and stop standard BABYLON.Animation objects that have been put into the .animations array of mesh/lights/cameras.

But that isn't what you asked.  :)

If I were you, I would send 'scene' in the parameters of your class constructor... like many BABYLON classes do.  Scene will already be established... by the time you are ready to instantiate an AnimationManager object.  Hope this helps.  If not, smarter people than I are nearby.  :)

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.

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.


  • Recently Browsing   0 members

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