Jump to content

Game object available to all modules with Node js


diegoalmesp
 Share

Recommended Posts

Hello guys!

 

I'm having some "problem" (not really, more a doubt). Working with Node js (firstime) I'am organizing an already made game. My problem is to acces the "game object" outside the modules.

 

So, how would you make the game object (var game = new Phaser.Game(SCREENWIDTH, SCREENHEIGHT, Phaser.CANVAS, 'game') ;) available to all the sub-modules in the app?. I'm sending the game as a parameter but I don't think this is a good approach.

 

This is what I have now: 

 

main.js: 

'use strict';// var PlayScene = require('./play_scene.js');var GlobalVariables = require('./main_variables.js');var MenuScene = require('./modules/menu/menu.js');var PlayerVsCpuGame = require('./modules/pvscpu/player_vs_cpu_game.js');var BootScene = {    ...};var PreloaderScene = {    ...};window.onload = function () {    var SCREENWIDTH = window.innerWidth;    var SCREENHEIGHT = window.innerHeight;    var game = new Phaser.Game(SCREENWIDTH, SCREENHEIGHT, Phaser.CANVAS, 'game');    // var game = new Phaser.Game(800, 600, Phaser.AUTO, 'game');    game.state.add('globalv', GlobalVariables);    game.state.add('boot', BootScene);    game.state.add('preloader', PreloaderScene);    game.state.add('PVsCPUGame', PlayerVsCpuGame);    // game.state.add('play', PlayScene);    // Main Menu For the Game    game.state.add('menu', MenuScene);    game.state.start('boot');};
Then the PlayerVsCpuGame (where I can access the game object easily):

var CpuPlayHardMod = require('./libraries/cpu_play_hard.js');var PVsCPUGame = {    handleResize: function(){        var w = window.innerWidth;        var h = window.innerHeight;        this.game.width = w;        this.game.height = h;        var ratio = 1600/900;         var windowRatio = w/h;        RATIO = w/1600;        if (windowRatio > TaTeTi._RATIO) {            TaTeTi._RATIO = h/900;        }    },    this.handleResize();    this.physics.startSystem(Phaser.Physics.ARCADE);    this.preloadBG = this.add.sprite(0,0,'generalBG');    this.preloadBG.width = TaTeTi._WIDTH;    this.preloadBG.height = TaTeTi._HEIGHT;    this.board = this.add.sprite(TaTeTi._WIDTH/2+TaTeTi._RATIO*30,TaTeTi._HEIGHT/2,'board');    this.board.anchor.set(0.5,0.5);    this.board.scale.setTo(TaTeTi._RATIO*0.35,TaTeTi._RATIO*0.35);    this.board.name = 'THE BOARD';    TaTeTi._GAMESTARTED = true;    this.HitZoneGroup = this.add.group();     // the group for player pieces    this.playerPieces = this.add.group();    // the group for cpu pieces    this.cpuPieces = this.add.group();....
 

As you can see on top of that file, I'm calling a new module, and here is where I'm having the problem:

// Hard Level CPUvar cpuPlayHard = function (game){    // if the game has ended then I'll skip this function    if(TaTeTi._GAMESTARTED === false) return;    // if all the pieces are on the board, then it will use the    // randomCpuKeepPlaying() function which moves the CPU pieces to    // keep playing    if(TaTeTi._COUNT > 5 ) { return false; }    self = this;    ...
because I need to call cpuPieces which is in the "parent module", and other methods as well. For now I'm passing the "game" object as a paremeter, but I intend to use more libraries like this one, and as I'm very new to node, I'm not sure how to do this in a right and clean way.

 

Hope you guys can help me with this. Thanks in advance!  :)

 

 

 

Diego.

Link to comment
Share on other sites

Some info is missing but why couldn't you just include the modules after setting var game, then just access it as a global variable named game? Unless you really want to use this. Have you looked at any templates for phaser games? https://github.com/photonstorm/phaser/tree/master/resources/Project%20Templates They don't got node.js but it should help. I might be missing something though.

Also why access phaser stuff with node.js, any specific reason? I use node.js for an express server, session stuff, login stuff, websocket stuff but no direct phaser stuff. Curious about its pros.

Link to comment
Share on other sites

Lol, just remove the var from it man, it will make the variable global :)  I use this same method for my gameserver.

 

Also to note, if you're wanting to add Redis / Scaleabaility for hundreds of thousands players later, you will need to change that global variable to get data from Redis from separate node instances. Keep that in mind

 

Also make sure you require the module after you assigned the global variables.

Link to comment
Share on other sites

Also why access phaser stuff with node.js, any specific reason? I use node.js for an express server, session stuff, login stuff, websocket stuff but no direct phaser stuff. Curious about its pros.

Thanks for answer! I'm using yeo-man generator and node to work with modules and compile them into one single file.

 

 

Lol, just remove the var from it man, it will make the variable global :)  I use this same method for my gameserver.

 

Also to note, if you're wanting to add Redis / Scaleabaility for hundreds of thousands players later, you will need to change that global variable to get data from Redis from separate node instances. Keep that in mind

 

Also make sure you require the module after you assigned the global variables.

 

Thanks! that seems to be my problem!

One more thing. As you can see on the second file I posted (PlayerVsCpuGame) I was adding stuff with 'this', and I need to access that in the third module (cpu_play_hard.js), should I add those functions to 'this.game' instead? would that be a right approach?. And yes, the idea is in the future to add a multiplayer instance, scores, etc so I need this to be as much "ordered" as posible. Thanks guys!!!  :)

Link to comment
Share on other sites

Thanks for answer! I'm using yeo-man generator and node to work with modules and compile them into one single file.

 

 

 

Thanks! that seems to be my problem!

One more thing. As you can see on the second file I posted (PlayerVsCpuGame) I was adding stuff with 'this', and I need to access that in the third module (cpu_play_hard.js), should I add those functions to 'this.game' instead? would that be a right approach?. And yes, the idea is in the future to add a multiplayer instance, scores, etc so I need this to be as much "ordered" as posible. Thanks guys!!!  :)

 

 

Not sure if I understand correctly (i'm a bit tired atm), but a trick that I do is I convert my functions to anonymous ones so I can use them as global functions in any required file.   For example, load in the functions before you require, and u can use them in the required js files. Does this kind of help?

 

So no, you wouldn't need to add them in every file

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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