Sign in to follow this  
oler

how to do Phaser + requirejs + nodejs

Recommended Posts

Good day to you all, I have just learned how to use requirejs this template really came in handy :

 

https://github.com/photonstorm/phaser/tree/master/resources/Project%20Templates/RequireJS

 

, now i am struggling to see how it fits in with server side code with nodejs I would like to incorporated most of my backend code from nodejs and I cannot find any examples on how to do this.

 

for example adding nodejs snippet inside of a module ::

define(['phaser'], function (Phaser) {'use strict';var http = require('http');                       <<<---------------- how am I supposed to incorporate nodejsvar fs = require('fs');var index = fs.readFileSync('index.html');               http.createServer(function (req, res) {res.writeHead(200, {'Content-Type': 'text/plain'});res.end(index);}).listen(9615);function Game() {console.log('Making the Game');}Game.prototype = {constructor: Game,start: function() {this.game = new Phaser.Game(800, 600, Phaser.AUTO, '', {preload: this.preload,create: this.create});},preload: function() {this.game.load.image('logo', 'assets/phaser.png');},create: function() {var logo = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'logo');logo.anchor.setTo(0.5, 0.5);}};return Game;});

I know node has its own server side require but it is very confusing how the backend require is supposed to work with the front end in the context of front end require. Can any body shed some light on this? Any examples will be much appreciated thank you.

Share this post


Link to post
Share on other sites

Nodejs is used on backend server side, and it has it's own module system, that is you don't use requirejs on the backend. On the other hand, Phaser, is for building games on the front end in the browser. You don't have the same module system in the front end so you can use requirejs on the browser side with Phaser.

 

So if you are building a Phaser game that works on browser you can use requirejs and not nodejs. For that I suggest take a look at this yeoman generator generator-phaserjs, that is for Phaser + RequireJS. 

 

But if you want Phaser to work on the server side, along with nodejs, you don't need requirejs. You can simply use nodejs module system like this:

// `File: MyGameState.js`module.exports = function MyGameState() {}

and require it like this:

var gameState = require('MyGameState')

Share this post


Link to post
Share on other sites

 

Nodejs is used on backend server side, and it has it's own module system, that is you don't use requirejs on the backend. On the other hand, Phaser, is for building games on the front end in the browser. You don't have the same module system in the front end so you can use requirejs on the browser side with Phaser.

 

So if you are building a Phaser game that works on browser you can use requirejs and not nodejs. For that I suggest take a look at this yeoman generator generator-phaserjs, that is for Phaser + RequireJS. 

 

But if you want Phaser to work on the server side, along with nodejs, you don't need requirejs. You can simply use nodejs module system like this:

// `File: MyGameState.js`module.exports = function MyGameState() {}

and require it like this:

var gameState = require('MyGameState')

 

 

thanks for the reply, i guess I wasn't clear enough. but here goes..

 

I am not trying to run require js on the server. I simple want to connect my phaser js front end game already using requirejs, to the backed nodejs code. I hope that simplifies what I am asking

Share this post


Link to post
Share on other sites

I suppose you're talking about isomorphic javascript here? You asked in private message about help, but I though about answering here. Frankly i don't have experience with Phaser + Nodejs, but maybe the requirejs node docs could be of help http://requirejs.org/docs/node.html  and try looking resources about isomorphic javascript.

Share this post


Link to post
Share on other sites

Require is not relevant to the connection between your game and its node.js back end. You can send communications from your game to node.js using XMLHttpRequest.

 

For example:

this.callServer = function(command, type, callback, data){        var formData = null;        if(typeof data != "undefined"){            formData = new FormData();            for(var s in data){                if(data.hasOwnProperty(s)){                    formData.append(s, data[s]);                }            }        }        var xhr = new XMLHttpRequest();        xhr.open(type, command, true);        xhr.overrideMimeType('application/json');//TODO - probably don't always do this!        xhr.onerror = function(e){            callback({success:false, err:e});        };        xhr.onload = function () {            var result;            try {                result = JSON.parse(xhr.responseText);            } catch(e){                result = {success: false, err: xhr.responseText};            }            callback(result);        };        xhr.send(formData);    };

and example useage:

//*************************************    // save project to server    //*************************************    this.save = function(projectData, callback){        var dataString;        var dataType = typeof projectData;        if(dataType == 'object'){            dataString = JSON.stringify(projectData, null, '\t');        } else if(dataType == 'string'){            dataString = projectData;        }        var data = {            projectData: dataString,            remoteDir: this.projectModel.folderPath        };        this.callServer('/save', 'post', callback, data);    };

Share this post


Link to post
Share on other sites

@oler, you can't mix your front end code with your backend code. Nodejs runs on the server, while your game will run on the browser. What are you specifically trying to achieve?

I am trying to build a p2p "chess - like" game and looking for the best approach to solve this issue. the main game engine is in nodejs and as you know front end in phaser. How would you approach this?

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.