Jump to content

Global Function How?


ramspeed
 Share

Recommended Posts

Hey there, I'm new to javascript and I'm trying to figure out how to create a global singleton for generic functions.

I can create global variables in the Boot state, but I'd like to create an object that holds global vars and functions.

I'm instancing a custom GameManager script in the index.html file like so:

//initialize framework
var game = new Phaser.Game(640, 960, Phaser.AUTO, 'game');

//create global object

var gameManager = new GameManager();

 

My GameManager.js file looks like this:

var GameManager = function GameManager()
{
    console.log('game manager created');
    
    this.myName = "adrian";
    
    this.GetName = function()
    {
        console.log("my name is "+this.myName);
    }
};

The problem is gameManager object is undefined elsewhere in game. Any suggestions?

 

Thanks

 

Link to comment
Share on other sites

Whats the actual error?

It simply looks like you're adding the `GameManager` constructor after it has been called, as it is split across files it wont even be hoisted (although V8 seems to have been a little funky with hoisting recently), so, I wonder if your actual issue is that `GameManager` is undefined.

To solve you just need to define your `GameManager` constructor some time before instantiating it.

A general rule of thumb is to include all constructors before any actual game logic, then work from there. This is a fairly naive approach and falls over quickly but for simple projects it can work. I expect this is the route you will take.

A better solution would be to use a module system of some sort. There is not a native one yet, but its possibly close, so if you're just learning that sort of stuff now start with commonJS modules as the native spec for modules is very very similar, there are some issues with actually loading modules to be ironed out but you can use a tool like browserify or webpack to help you out (there are others).

 

Link to comment
Share on other sites

Mattstyles: I'm getting an undefined error.

Shohan: I am able to create a global object using your approach:

game.global = {
        
        monkey: 15,
        
        gameManager: new GameManager(),
          
        TestIt: function()
        {
            console.log('Shohan');
        }
    
    };

This allows me to reference gameManager elsewhere like this:

this.game.global.gameManager.doStuff();

Does that seem like a viable approach? Thanks for the input guys.

 

 

 

 

Link to comment
Share on other sites

If you still want to do it the way you did and use global varibale in windows scope, then you just need to do it the way mattstyles adviced you - just change order.

Instead of your:

var gameManager = new GameManager();

var GameManager = function GameManager() {...};

Make sure you have it in this order:

var GameManager = function GameManager() {...};

var gameManager = new GameManager();

This works perfectly fine unless I missed something (but I tested it in jsfiddle just to be sure I didn't miss anything ;-)).

Btw in js I don't thikn you need to specify function name provided you keep it in var, so you can just go with var GameManager = function() {...};, this should be the same thing.

If you wanted to do in your order of things then you would need to go with a function not stored in a variable with just function GameManager() {...}; then it doesn't matter when you call this as a new constructor though not sure if it goes exactly in your singleton way.

 

EDIT: not sure why but it doesn't show the code tha same way as yours guys, though in edit it's in that dark background styled rectangle.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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