Making Realtime Multiplayer game with Starling in Flash

Since the flash port of our AppWarp SDK has been released, now you can easily develop real-time multiplayer games for flash platform. The AS3 SDK of AppWarp does not restrict you in anyway while developing games in flash. You can easily integrate our multiplayer SDK with any present flash game engines like Flixel, Starling, FlashPunk, etc

In this tutorial, we will see how you can integrate AppWarp SDK with starling framework.

Starling is a 2D game framework written over the Stage3D API and is available on both Flash Player 11 and Air 3. Starling is based on Sparrow Framework for iOS which was created by the same developer.

In this tutorial, we will be creating a basic multiplayer version of the classic game ‘Snakes’. The sample game is named as ‘Snake Wars’ and is open source and is available on github. Source code of Snake Wars is available and can be downloaded or viewed from our git repo.

 Making Realtime Multiplayer game with Starling in Flash

The game has both single player and multiplayer modes. The multiplayer code is written in Multiplayer.as file and AppWarp related code is written in AppWarp.as

In multiplayer.as, you will notice that two arrays named snake have been created and snakeremote to represent the local player and remote player

1
2
private var snake:Array;
private var snakeRemote:Array;

The default length of snakes is 4 blocks with each block of size 16 pixels. I have only sent the location of the first block while other blocks are being calculated from the adjacent blocks. Similarly, I have only sent the information when the snake eats and grows. So, there are three types information that I am sending

1. Begin

2. Move

3. Eat

Connecting To AppWarp Server

The very first step, to implement the multiplayer functionality, is to connect to the AppWarp Server. To do the same, first initialize the WarpClient and get the instance of client;

1
2
3
4
5
var APIKEY:String = "90d50de6c0e7fb3c887f5a6317a4c7695f0121ffe6051f2f9ad28349ff9e2a09";
var SECRETEKEY:String = "bb0df91101188da293c72949c92237386076fe6c18db14577d046e3cba95d6bc";
var client:WarpClient;
WarpClient.initialize(APIKEY, SECRETEKEY);
client = WarpClient.getInstance();

Next you need to define classes for these listeners and create objects and attach them to the WarpClient.

1
2
3
4
5
6
7
8
9
10
11
12
public var _roomlistener:roomListener;
public var _zonelistener:zoneListener;
public var _notifylistener:notifylistener;
public var _connectionlistener:connectionListener;
_roomlistener = new roomListener();
_zonelistener = new zoneListener();
_notifylistener = new notifylistener();
_connectionlistener = new connectionListener(f);
client.setConnectionRequestListener(_connectionlistener);
client.setRoomRequestListener(_roomlistener);
client.setZoneRequestListener(_zonelistener);
client.setNotificationListener(_notifylistener);

All these classes and functions have been defined in appwarp.as only.

MatchMaking

The next step after connecting to the AppWarp server is to find a suitable player to player. For this, I have used the client.joinRoomInRange() function of WarpClient. Here first I will check if there is a player already waiting for another player by sending minimum and maximum number of users as 1 and 1.

1
client.joinRoomInRange(1,1,true);

When no such player is found, we simply create a new room and join it and wait for some other player to join as well. Once we join a room, we have to subscribe to room as without subscribing you won’t be able to get notifications and chat messages related to that room.

The GamePlay

Once you have found the player, it’s time to play the game. The information, while the game is going on, will be exchanged in JSON format. I do not send the information every time, I will only send the information when a player begins to play, changes direction or eats food i.e. when it grows.

1
2
3
4
5
6
7
8
9
10
11
12
public static function send(type:int,dir:int,x:int,y:int):void
{
    if(Connected == true)
    {
        var obj:Object = new Object();
        obj.type = type;
        obj.dir = dir;
        obj.x = x;
       obj.y = y;
client.sendChat(com.adobe.serialization.json.JSON.encode(obj));
    }
}

As you can notice, with every message, I am sending the type of message i.e. begin=0, move=1, and eat=2, the direction and the x and y coordinates of first block of snake.

When a player leaves the room or gets killed, the other player is considered as a winner.

Please view the whole source code of game available on github to understand how you can easily integrate AppWarp SDK with Starling framework. Please send your feedback to us on support@shephertz.com

twitter Making Realtime Multiplayer game with Starling in Flashfacebook Making Realtime Multiplayer game with Starling in Flashgoogle Making Realtime Multiplayer game with Starling in Flashlinkedin Making Realtime Multiplayer game with Starling in Flashpinterest Making Realtime Multiplayer game with Starling in Flashreddit Making Realtime Multiplayer game with Starling in Flashstumbleupon Making Realtime Multiplayer game with Starling in Flashtumblr Making Realtime Multiplayer game with Starling in Flash

2 thoughts on “Making Realtime Multiplayer game with Starling in Flash

  1. Hi,

    I am looking for some multiplayer games which needs to be run in real time and can be rendered to users to play with each other. Please let me know which technology is best suited for this requriement? i was reading on node.js and express framework but after this article i am confused with how far can flash games suffice my requirement?
    Also anyone who has such games readily available / develop, please let me know.

Leave a Reply

Your email address will not be published. Required fields are marked *


six − 1 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>