Archive for the ‘ActionScript’ Category

Creating a chat in Flash is easy using ElectroServer and MXML. If you are interested in creating multiplayer games, then first creating a simple chat application can help you learn the basic API calls used to communicate with ElectroServer. After building something like this you will have enough confidence to take the next step toward making a multilayer game!

At a high level you do the following to create a chat,

  • Download and install ElectroServer to get it running on your machine.
  • Create a new Flash project that uses the Flex Framework so that you can easily add UI components.
  • Add a few dozen lines of code to hook the ElectroServer API up to the UI.

This tutorial shows you how to create a chat step-by-step.

View the live chat example here.

Read Full Post »

After many months of hard work by the team at Electrotank, I’m pleased to say that ElectroServer 5 (ES5) is out the door and ready to power tons of new multiplayer games and MMOs! It is available now for download and comes with a free 25 concurrent user license (with no other restrictions). In addition to the great new features with the 5.0 release, we have a ton of additional features that we’ll be adding and releasing over time.

ElectroServer 5 website

Press release

Here are some of the most notable new features.

Cross platform APIs

Over ElectroServer’s 8+ year life it has primarily been used for Flash applications. We’ve now introduced APIs across several languages keeping them as similar as possible. You can now have players play against each other across a multitude of platforms. The supported languages and platforms are,

  • AS2 and AS3 – Flash, AIR
  • C# – Unity3D, .Net, and XNA
  • Objective-C – iOS
  • Java – for any Java enabled client


For the fastest and most light weight message transfer, ElectroServer 5 provides the option for UDP messaging. UDP is needed for first-person shooters and racing games.

Message Encryption

ES5 supports strong packet encryption using the industry’s highest standard AES-128 method.

Message encryption is just one extremely effective means in which ES5 secures client-server communications. ES5 also supports cryptographically hashing passwords used to log onto the system.

Full-featured Sleek Admin

ElectroServer’s admin tool has been completely revamped to be more user-friendly (and attractive!). With it you can configure every aspect of the server. In addition, there are new reporting features built right in. You can view and configure graphs showing up-time, bandwidth usage, CPU usage, and more. There is also the ability to browse snapshots of the server to inspect which rooms exists and who is in them.

Extensive Documentation & Tons of Examples

We invested a lot of time in creating thorough documentation for every server feature. There are tutorials and more than 30 examples! ES5 also comes with a handful of complete multiplayer games.


Screenshot of ES5 game example



Screenshot of Es5 game example


I hope to post more on ES5 and multiplayer game development in the near future.

Read Full Post »

Bresenham’s line algorithm is a fairly simple algorithm that takes two tile positions in a grid and returns a list of all tile positions that a line drawn between the two positions touches. It is well described along with easy-to-read pseudo code here.

When is this useful? According to what I’ve read, this algorithm and variations of it are useful in graphics programs. I’m sure you’ve seen programs that let you draw icons in a zoomed-in view. While you draw lines it just colors in boxes. It is likely this algorithm was used for that. However, I use this algorithm for game-related purposes. The last time I used it was probably 5 or 6 years ago in a Flash pool game. I set up a grid with an arbitrary tile size on the pool table (that the user can’t see). Every frame I determined in memory which tiles a ball covered as it moved (assuming no collisions). Then I would only run collision detection against balls that touched the same tiles. This let me cut down on the number of collision checks per frame.

I’m coming back to this algorithm again now because of a first person shooter game I’m working on. I hope to create a blog post on how the algorithm is used in that game (on the server) to keep the collision detection calculations to a minimum.

I created a Flash demo of Bresenham’s line algorithm that you can play with by clicking on the image. Or you can download the source files here.

click to view example (drag dots around)

Read Full Post »

I recently spent a little time recreating some older spring physics code, in AS3. It is the sort of thing you’ve probably seen 100 times over the years, but working on stuff like this never gets old to me. I’m sure I’ll recode it again in a few years 🙂

In this post you’ll see a quick demo of the spring behavior, a link to the source, and a little bit of explanation of physics.

click to view example

A little explanation
Spring behavior can be coded by first understanding Hooke’s Law. Imagine that you had a spring and a mass attached to the end of that spring. Hooke’s Law specifies the force that the spring applies to the mass when displaced from it’s rest position.

F = -k*x

Where x is the amount of displacement from the rest position, k is the spring-specific constant (spring “tightness”), and F is the force exerted by the spring on the mass. The force tries to get the mass to return to the rest position, so it is in that direction.

In my code I use the Vector3D class to generalized the equation from just x to x and y. Also, I connect two equal masses with a spring rather than just having a spring with a single mass. Two masses and a single spring obey the same equation. It is just that the two masses have the force applied in opposite directions.

F = -k*displacement

Where displacement is the distance between the two masses minus their distance at rest. If F is applied to one of the masses, -F is applied to the other one. The minus sign can be manually applied to wrapped into the displacement if calculated relatively.

A single mass can be connected to N springs. To achieve the correct resultant behavior the forces acting on each mass need to be summed each iteration and then used to affect the velocity.

What is missing?
I am not properly letting the system lose energy. To properly lose energy the springs should apply a resistive force against the direction of movement of the mass in the amount b*velocity, where b is a spring-specific constant. Currently I’m just slowing all things down by a factor of ~.95 every iteration…sort of like if the whole system was in oil.

Also, I’m currently assuming all masses are equal. This could be easily enhanced to support varying masses.

What is it good for?
This type of behavior comes up from time to time in games. Off the top of my head I remember using this behavior to simulate how a car shakes due to breaking quickly (from side view). The game was Pimp My Ride: Crowd Magnet. I think I created a rectangle with 4 masses and 6 springs. The springs connected the perimeter and the two diagonals. I hid the visibility of this frame but had it move on screen along with the car. I used the two top nodes to tell how to move/rotate the frame of the car as it stopped.

There was also a non-game UI that I created years ago for a client similar to what you see at Visual Thesaurus. Nodes were connected via springs, but then the nodes themselves also had a gravitational pull toward each other and a slight magnetic repulsion. It creates an interesting interface!

You could use a spring system to mimic a blob or cell. Turn off the lines and nodes and fill it with a color and allow it to bounce off of things. Or make a chain/rope that your character can swing from. Uses for this pop up from time to time!

Read Full Post »

This post is related to a post I made a few days ago titled Ball Bouncing off Line Calculation.

The code used to determine the collision reaction of a ball bouncing off of a line shown in the previous post is the same as the code required to do it in 3D! So, you can achieve a ball bouncing off of a plane. Or, when you add gravity plus a little damping on the floor you can have things rolling around.

This example was designed so the ball would hopefully roll for a long time without rolling off. But if the ball was going a little faster you’d see how it can bounce into the air a bit if it hits an incline fast. Anyway, just thought I’d share!

(marble madness anyone?)

Note: if it seems a little slow, it is probably the 5 other blogs on this page that are running more intense things.

click to view example

Read Full Post »

I recently finished writing ActionScript for Multiplayer Games and Virtual Worlds, my first book since Flash 8! With the help of some amazing contributors I think this is the most useful and geeky-fun book I’ve ever been a part of. According to Amazon (shameless link) it hits shelves in 8/3/2009.

So what’s it about? If the title of the book doesn’t give it away, it is a book that focuses on multiplayer concepts and applies them to games and virtual worlds via ActionScript. Some of the coolest topics are those on dead reckoning, real-time movement and time synchronization, loads of information on avatar rendering, and other useful topics like thorough coverage of isometric concepts.
While there are many examples throughout the book, there are 3 major examples. In one chapter we build a real-time multiplayer tank game (top down). In another chapter a cooperative multiplayer games is created, and its a fun one! Then over the course of several chapters we dig into various major topics on virtual worlds to build a complete world.
Here are some screenshots, and I hope some of you check it out and find it useful 🙂
Virtual world
[image missing]
Real-time tank game
Cooperative multiplayer game

Read Full Post »

I’m working on a new game that requires me to dig into some physics and math that I haven’t touched in a while. The Vector3D class was introduced with Flash Player 10. I used this as an opportunity to get familiar with that class and see if it could make life a little easier, and it does!

The Vector3D class keeps track of x and y (and z if you want it to) values. It uses the sign on the value to imply direction. You can then perform cross products or dot products and a few other basic vector actions via its methods.

Conceptually, here is how to make a ball (or sphere) bounce off of a line (assuming you can calculate that the collision is occurring).

  1. Use a vector for the ball velocity
  2. Use a vector to represent the line normal (perpendicular to line)
  3. Do simple vector algebra to get new velocity

Step 1
Represent the ball velocity as a vector. For instance,
var velocity:Vector3D = new Vector3D(2, 0);

Step 2
Represent the line normal as a vector (which it is). For instance,
var normal:Vector3D = new Vector3D(1, -1/line_slope);

Step 3
Do the vector math when a collision is detected. You can imagine that the velocity vector can be projected onto the normal, and it is only that bit that will be reflected off of the surface. So, we’ll project it onto the normal and set the net change to 2 * this value. 1* the value makes the velocity 0, 2 * the value sends it back an equal amount. We then take this reflected vector and subtract from the incident velocity vector.

var reflectionScalar:Number = 2 *velocity.dotProduct(normal);
var reactionProjection:Vector3D = normal.clone();

Download source

click to view example

Read Full Post »

Older Posts »