I’m making a 2-player fighter Flash game that has TCP connection, so you can play against another player on a totally different computer. It works, and all of the signals successfully send and get received from each other, but after a while of playing, eventually the characters on the screen go out of sync. Like, on one client, a character will be towards the edge of the map, and other the other client, the same character is in a completely different spot. I don’t understand why this is, considering the same exact messages are being sent to both clients. I heard I have to sync up the clients with a “tick rate” or something? I’m completely lost on this, anyone have any ideas? Thank you.
Yeah, the simplest way to handle the problem is to periodically have the server (there must be a server or a master client if you don’t want to delve into more complex topics) broadcast the global state that all clients must display in order to be correct. So if you’re sending messages like “walk RALPH forward 5 pixels”, your master broadcaster needs to send messages like “RALPH’s absolution position is 305” at some rate that mitigates whatever drift you’re seeing.
There are a bunch of timing issues that Flash doesn’t help you with at all here. Player 1 may receive three messages at once, and Player 2 may receive them three seconds apart. If you’re applying any amount of gravity, friction, acceleration, or any sort of time-based logic in your game, things are bound to get out of sync. Without going into details in this short post, there are an absurd amount of things that Flash, or AS3, or TCP aren’t going to guarantee when communicating with multiple players.
The problem you’re butting up against in general is very common in computer science (and game programming), and there are many ways to solve it for various types of applications. Games tend to rely on the master server approach to 1) prevent cheating and 2) make things easy to program and reason about. But, there are tons of interesting things to read about on the topic if you’d like to do so:
Creating engaging and entertaining content for designers and developers since 1998.