I’ve not posted in a while due to travelling for three weeks in Argentina, experiencing all I could of the Beef, Wine, and spectacular scenery.
However, I hadn’t let myself go too far from a keyboard of an evening and carried on working on my Phaser project.
The previous post finished on the point where I wanted to control the game remotely from another device; a mobile phone and again Phaser provided just what I needed.
The main app moved the paddles by calling functions to move the paddle up… or down, so all I needed to do was to call these functions from elsewhere, easy? right?
From previous work on ‘The Cube’, I realised that using WebSockets would be a great way to send messages to call these functions. and wrote up some basic lines of code to allow messages to be sent from one user to another.
Normally this sort of system would be a Client / Server setup, but in this project, the system evolved differently; the game was a client that listened for certain messages from the controlling app client, but it also sent various messages itself, such as score, winner and what paddles could be chosen.
I think the best part of this was the relatively ‘simple’ user authentication system:
The problem I had fairly early on was that a user could choose to play as the left paddle, but if another player connected, they could also choose the left paddle and all hell would break loose over control of the poor paddle.
To resolve this, I created a ‘Spaces’ array which stored the available spaces for the current game. When a new player connected, the remote would first check what spaces are available and display the available choices to them, once picked, the new ‘Player’ is placed into the ‘Spaces’ array with their WebSocket connection ID as a unique identifier.
This became helpful two-fold because if a player disconnected, the WebSocket library would emit this action and the game would then remove that ‘Player’ from the ‘Spaces’ array to allow another person to join.
So after many evenings working out logic, code, and using multiple windows on a small 13″ Macbook screen I was able to create a game which allows a user to connect to The Cube and play Pong from their phone! 🙂
And yes I’ve now fixed the typo in ‘lets’ :-p