Lennaerts PHP mysql multiplayer tutorial

4
  • 42 favourites

Index

Attached Files

The following files have been attached to this tutorial:

.capx

multiplayer.capx

Download now 501.64 KB

Stats

11,963 visits, 28,405 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Hi folks, this is the tutorial for a php based multiplayer, build from scratch.

It is loosely based on my TankWars multiplayer test game.

This method works with CocoonJS too as they are mere ajax request.

Just 26 events ^_^

Check the Capx for details on events.

Here is a working demo made with this tutorial

When entering the game, a user sessions code will be tied to the users browser for indentification. (meaning a unique code will be stored in webstorage)

The PHP files are included in the CAPX

Required:

Host location with PHP (any version php will do really, with mysql support)

A mysql Database to which you have access rights.

The general flow of information is as following:

Data flow information

Layout starts:

Data is requested about active players and positions,

Players are placed in position

Updates about player positions are requested, and repeated as soon as processed.

Updates contain players information, shots fired, game messages and player state.

Shots fired by the player sent their own ajax post.

Enemy killed also sents its own request, but the player is not kill until the next update processed his death information back to construct 2 from the database.

This is basically it, you have positions, and they require updating.

Next to that there will be some extra activity, such as shooting and players dying .

In order to keep this fast and have an as close sync with other players as possible some elements are crucical to keep in mind;

Speed: If things move too fast, players will seem to make big jumps after an update, keep it slow so this difference does not become annoying. Ofc we could add some Lerp or movement effect to an update, but I realised the animated movement will be done when the player already moved making it less realistic. So therefore, instant updates.

Requests: We could ofc do requests for every type of actions, like movement, and shots, but it would degrade the speed of your requests, seeing as requests are processed linear. (one by one in a row) So to overcome this, we will stuff as much data in a request's reply as needed, and limit the amount of Ajax post requests to 1 per go as much as possible.

Setting up the game Layouts

I use 2 layouts for the game elements, gamelayout and objectslayout.

I use an eventless objectslayout to store all my objects which are not directly needed on game load. This has lots of advantages; which I will not go into depth here.

The bare minimum which I will be using are.

GameLayout:

Blue squares (spawnpoint): will become spawning points, this is 1 object, spawned 4 times(copy pasted) manually somewhere on the map.

Black sqaure (playerbase): This is the base of the active player, all motion will be directed through here.

Triangle (player): This will be representation of the player.

Textboxes at the top: 1 for how often your killed, 1 for kills made, 1 for game messages.

ObjectsLayout:

Triangle (enemy): This is a clone from the player, but a different object with its own name.

Explosion (explosion): we will use 1 explosion deaths

Bullet1: Bullets shot by the player

Bullet2: Bullet shot by other players on player map

There is a need for having 2 sepperate bullets which almost do the same thing, this to accomadate for multiplayer mechanics simplicity.

From the current objects only the enemy triangle requires a set of instance variables.

The Bullets each require the instance variable 'shooter' indicating who shot the bullet.

I gave the explosion a fade out effect of 0.5 seconds which gets destroyed after fade out.

The messages text will get a fade effect which does not get destroyed after fade out.

Kills and Killed remain plain.

Thats basicallly it for the layouts, clean and simple.

Next page, setting up our database.

  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!