Realtime Multiplayer games are very popular and form a great part of games being developed in the market today. The backend for such applications is very important as it provides support for real time data transfer amongst the clients and is the fundamental building block for them. Creating a multiplayer game is not an easy task because besides the Single Player logic, user has to maintain commuication between devices.
Developing Multiplayer games on entry level smartphones becomes really challenging due to limited resources on devices like RAM and processor and jar size limit.
AppWarp is a lightweight and powerful, yet simple platform for developing multiplayer real-time games and applications. It provides features which help developers to build seemingly complex applications with little effort. It seamlessly works across multiple client platforms.
Here are the steps in S40/J2ME on how to integrate AppWarp in your Midlet:
Create Midlet
public class StartMidlet extends MIDlet implements ConnectionRequestListener{
…...
}
midlet is the start up class for any J2ME mobile app whether it is in j2me or LWUIT or Nokia asha.
ConnectionRequestListener has been implemented in this class because we will try to connect to appwarp server in it and we can move forward once this is connected.
Defining Variables:
private Display display;
private Mycanvas mycanvas;
private String API_KEY = "";
private String SECRET_KEY = "";
private WarpClient warpClient;
You can get your api key and secret key by registering with ShepHertz
Instantiate Display and Appwarp
// Constructor
public StartMidlet() {
display=Display.getDisplay(this);
try {
WarpClient.initialize(API_KEY, SECRET_KEY);
warpClient = WarpClient.getInstance();
} catch (Exception e) {
e.printStackTrace();
}
mycanvas = new Mycanvas(this, warpClient);
}
This is to initilize AppWarp and mydisplayable class
Try to connect in startApp
/*
* adding connection listener before connecting to appwarp server
*/
warpClient.addConnectionRequestListener(this);
warpClient.connectWithUserName("Saurav");
As startApp is the life cycle method of midlet and it runs just after the constructor, I have added ConnectionRequestListener to specify the result of call back in same class.
Getting result in call back methods
public void onConnectDone(ConnectEvent event) {
if(event.getResult()==0){// success case
display.setCurrent(mycanvas);
}else{// failure case
System.out.println("Connection Failed: error code "+event.getResult());
}
}
public void onDisconnectDone(ConnectEvent event) {
}
We will get the result of the API execution whether it is a success or has failed in onConnectDone. In this step, we are getting ConnectEvent as a parameter with which we can check if the connection state is successful or not. If it’s a success case we will go forward to canvas.
Sample Code:
https://github.com/shephertz/AppWarp_JME_SDK_JAR/tree/master/WarpSample_J2ME
It would be great to hear the views from the readers of this blog. In case you have any more questions, please feel free to reach out to us at support@shephertz.com.
Leave A Reply