Jump to content

Making Global class


uyerdna
 Share

Recommended Posts

Hi everyone

 

Coming here as AS3 developer, I have almost zero JS experience previously

Perhaps what I'm missing is proper JS coding knowledge, I'm sorry if I posted at the wrong section

 

I'm building a test project using latest Phaser. So far so good! Have managed to create some things visible, tween, bitmaptext and spritesheet working, and learn a few tricks in the way. My only complaint is that coding Phaser JS on Sublime Text is a pain when compared with coding AS on FD and I'm too lazy to give Typescript a try.  :rolleyes:

 

Currently I'm using the template from the nice codevinsky

http://codevinsky.ghost.io/phaser-tutorial-getting-started-with-generator-phaser-official/

 

Very helpful to add new States easily and made my life easier with its pre-configured grunt task.

 

My problem now is how to make a Global class so that I can access it from anywhere. In AS3, I can easily create a new generic class and use it anywhere else. I'm sure this has something to do with the way JS works, but I really want to write customer's game logic in a separate file (TCustomer.js)

 

I really have no idea how to include / import it to my play.js, if it's at all possible. Have tried a few ways, but none were successful.

 

So far here's my code: 

window.onload = function () {  var game = new Phaser.Game(800, 480, Phaser.AUTO, 'phaser-sandwich-test');  // Game States  game.state.add('TCustomer', require('./states/TCustomer'));  game.state.add('TSaveManager', require('./states/TSaveManager'));  game.state.add('TWindow', require('./states/TWindow'));  game.state.add('boot', require('./states/boot'));  game.state.add('globals', require('./states/globals'));  game.state.add('menu', require('./states/menu'));  game.state.add('play', require('./states/play'));  game.state.add('preload', require('./states/preload'));    game.state.start('boot');};

With the code above, I can access TCustomer and TWindow anywhere I want. But the problem is, they are both NOT game states. I'm pretty sure there must be a better way to do this. Will there be a problem if I keep adding new global classes as game states?

 

Thanks in advance!

Link to comment
Share on other sites

Looking at the code on GitHub, it should just be a matter of calling --

$yo phaser-official:prefab "prefabName" 

-- with the name of the Phaser.Sprite subclass you want to use.

 

Alternatively, you can define your class in a file and then call require() on it. (Using Node.js + grunt to create it.)

 

Everything runs within the function scope it is created within in JavaScript too. If a class was instantiated within the window.onload function, and existed before the calls to game.state.add, all of the states could reference it. So, if you wanted global classes (singletons), either its class definition or an instance of it needs to exist before any code that references it is called.

 

For example, something like the following:

window.onload = function () {  var game = new Phaser.Game(800, 480, Phaser.AUTO, 'phaser-sandwich-test');  var TCustomer = require('./states/TCustomer');  var TSaveManager = require('./states/TSaveManager');  var TWindow = require('./states/TWindow');  // Game States  game.state.add('boot', require('./states/boot'));  game.state.add('globals', require('./states/globals'));  game.state.add('menu', require('./states/menu'));  game.state.add('play', require('./states/play'));  game.state.add('preload', require('./states/preload'));  game.state.start('boot');};
Link to comment
Share on other sites

TCustomer.js

=====================

 

var MyGame = MyGame || {};   //Create the MyGame namespace if it doesn't exist (it's just a variable holding an empty object)

 

MyGame.TCustomer = .....    // Here you can use object notation, constructor functions, IIFEs or any value depending on what you want

=====================

 

Then for TSaveManager.js you do the same but using MyGame.TSaveManager instead of TCustomer. That way you use only one global variable for you game and put everything inside it. It's similar to using packages in AS3.

http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html

Link to comment
Share on other sites

Thanks for the answers guys!

You're all very helpful!

 

+1 for Videlais

this is exactly what I was looking for

 

 

 If a class was instantiated within the window.onload function, and existed before the calls to game.state.add, all of the states could reference it. So, if you wanted global classes (singletons), either its class definition or an instance of it needs to exist before any code that references it is called.
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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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