Cocos2DX Sample Application

This wiki guides you through the code for Ninja Shoot Cocos2dx demo game.

The tutorial itself is quite simple. The user first needs to click on the
“Start Game”. Once started, the scene changes to the game play scene.
Since its a 2 player demo, another user will also do the same steps and
voila! they can shoot balls at each other by tapping on the screen and
see them move in real-time.

Now lets understand how the game code works by examining the key code
snippets. Entire source code is available and can be downloaded or
viewed from our git repo.

First we specify the AppWarp constants that will be used in HelloWorldScene class,

You get your values by registering and creating an AppWarp type of application from AppHq (Shephertz developer dashboard). This page contains a step-by-step guide on how to register, creating the app and a
game room. Once you’ve got these values, replace as indicated.

Next we simply add the required UI elements (local player, remote
player etc.). For the real-time communication to happen, you need to use
AppWarp source code. The latest version can be downloaded from our Git Repo here.
Add this source code to your project. Next you need to initialize the
AppWarp’s Client singleton and set up your listeners through which you
will get events.

The listener callbacks required for this sample are defined in
HelloWorldScene.cpp file, so HelloWorldScene itself is added as a
listener in the above code. ConnectionRequestListener defines the
callbacks for connection establishment and tear-down.
RoomRequestListener defines the callbacks for the game room’s join and
subscribe operations. NotificationListener defines the callbacks for
remote notifications such a player sending a chat or updating a rooms
properties etc.

Now we are ready to connect to AppWarp cloud and start our game. Now
each user that is concurrently connected to your game must have a unique
username. In this demo we will simply use a random user name for this
and connect. You can use other solutions such as a facebook id, email
address etc. to guarantee uniqueness depending on your game.

The joinRoom is called in the Listener onConnectDone and the game
starts when onJoinRoomDone is called with a success response.
Now the user can shoot in the opponents direction simply by a touch in
the direction he want to shoot which is handled as follows:

Here We are sending x and y values of the touch point according to
the remote player’s frame of reference as well as the duration in which
the projectile need to go beyond the screen. The data is a string where
characters “x” and “d” are used as separators. This message is received
by all players in the room and they can then update their UI in turn.
When we receive remote messages we need to write some code to update the
remote player’s position or projectile depending on the message
received. Here is the code snippet which handles it in the
HelloWorldScene.cpp file