Jump to content

Attempting a multiplayer game using Phaser


Mariusz
 Share

Recommended Posts

Hey there.

I'm fairly new to making games, let alone making them using Phaser.js. Sorry if this question is outright stupid or silly for one reason or another, I really don't know much so I thought I'd ask here.

I have recently started playing with Node and socket.io and it got me thinking.. how would I go about combining this and Phaser.js? Is there a particular set of steps that I have to undertake in order to make this work? At the moment I'd like to just have a sprite rendered using Phaser on each client and give them an ability to connect to each other (sort of like in this example, which I was using to teach myself the basics btw.). 

Again, sorry if it's a silly question, I haven't been doing this all for very long. 

Best regards and keep up the great work!
Mariusz

Link to comment
Share on other sites

Hi, in a very simplified way, it goes something like this:

 

  • Player 1 browses to the game -> socket.io connects to the node server which manages a list of connected players and their properties such as position -> make the player's sprite visible.
  • Player 2 browses to the game -> socket.io connects to the node server which sends a broadcast to all connected players notifying them that player 2 has connected and sends a message to player 2 telling him the properties of all connected players -> make all connected players' sprites visible and apply the properties onto them.
<script type="text/javascript">(function () {	var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });        var p1, p2;	function preload() {		game.load.image('mushroom', 'assets/sprites/mushroom.png');	}	function create() {		p1 = game.add.sprite(0, 0, 'mushroom');                p1.visible = false;                p2 = game.add.sprite(0, 0, 'mushroom');                p2.visible = false;	}        var socket = io.connect('http://localhost/');        socket.on('connect', function () {           p1.visible = true;           socket.on('message', function (msg) {              if (msg === 'p2connected') {                  p2.visible = true;              }              //all of your other logic for moving players etc..           });        });	function update() {           if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {               p1.body.velocity.x = -150;               socket.send('moveleft');           }           //etc..	}})();</script>
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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