Skip to content

3 player pong doc

The game is just about finished now apart from the reset and restart buttons which i would like to try and figure out how to use these.

I might post the fla on my site soon over christmas. For now im just going to let you know what is does and little bit into how.

Player view

This is a three player game of ping pong. The first screen you will see is the welcome screen with a start button and a place to input your name if you wish. I’ve left it so you can choose to input a name or not, when I was testing it many couldn’t be bothered to type it in.

Once you have pressed the start button you are brought to the main game stage.
There are three buttons; the two bigger buttons allow the user to choose which side they want to play on.
The third small button at the top of the screen is for a third player to play as the paddle in the middle of the screen as a blocker. This blocker can’t win the game or score any points but they can be a nuisance to either side during the game. When the third player joins the game they don’t get the option to input their name as they aren’t able to score any points.

As the player chooses either player 1 or player 2 start buttons then the other buttons are disabled and the side of the screen they have chosen is then revealed.

The game is very simple in that the two main players have to stop the ball from going past them by moving the paddle which is controlled by moving the mouse.

Then each of the two main players score points when the other player misses the ball and it goes behind them. The first player to score 10 points wins the game.

If any other players join the game they are instantly taken to the game and they can watch it.

Code view & Performance

I’ve commented the code throughout but here is an overview of what is happening.

I started by looking through the Flash Communication Server sample files especially the tutorial on the Shared Ball. This was very useful to learn how to set up the connection to the Flash Communication Server. I set up the server on my own computer and I was able to run the game using localhost. I then practiced by passing things using the shared object. The rest of the game is mostly normal flash scripting that I usually do just combined with the shared object so it can use the server.

I figured out that by getting the user using the whoAmI variable we can use this to our advantage to find out who they are and then do certain things only on that computer or only for them. This way you don’t need to get everything for everyone’s movie they just get the details they need. This also helps with any bandwidth on the server as there is less data being transmitted to the movie. As this game is very small this isn’t an issue but in larger games it would be.

The game is made up of three frames; the first contains the intro screen and player name input. The second one contains the actual game elements, and finally the third frame contains the winning page when the game is completed.

The first frame preloads the movie and sets up the connection to the Flash Communication server and creates the remote shared object named “pong” that will be used to store all the game variables. I’ve set the parameter to false so the Shared object is then temporary. If it was set to true it would be stored on the server when the user logs off.

I’ve have included some checks to make sure that the connection has been made and if not the user is alerted. I have also used Flush to make sue there is enough room to store the shared object.

Once we know the connection has been made and the shared object is initiated we hit the game itself.
The entire game is based on sharing the remote shared object with other players. Any time the object is changed or updated by one player, another player has to see the result. The onSync function is used to check for any changes to the shared object. As soon as a change is made the Flash Communication Server synchronizes the shared object.
To simplify things, the ball logic that controls and detects collisions between the ball and paddles happens on player one’s machine, then the values are sent to the other players through the shared object. This means that there is only one player’s movie doing all the workings on the game and the other players just need to use the shared object to get the results such as where the ball is or where the other players paddles are. This should help with the running of the game on the server as you aren’t getting multiple players sending the same information. Each player just sends their own details of their paddle and then gets where the ball is and where the other paddles are using the shared object.
With the game running over a more than one computer and more than one user accessing the same information on the flash communication server there can be some delay in the game. When testing the game I did find if the game was played over a wireless network it would have more of delay due to the wireless network not being able to transmit the data fast enough.

By having one player do all the working out for the game though this helps to minimise the amount of info that is needed to be sent to the shared object.

The only thing I was unable to do was to restart or replay the game. I used the clear(); on the shared object but as there is more than one movie being used at once I think that the shared object is the re-synchronised and the game is unable to restart with out actually unloading the movie because the data is immediately sent back to the user.