Making Turn based Game using App42 Corona SDK

We released App42 Corona SDK last month and got lots of query about writing turn based asynchronous game for the same. We developed a sample turn based Tic-Tac-Toe game using our Corona SDK for android platform and can be used as a prototype for writing other turn based games. This sample uses Push Notification/Storage and User Service to implement turn based game. Here are few and easy steps to get started with this sample.

Running Sample:

  • Register with App42 platform.
  • Create an app once you are on Quick start page after registration.
  • If you are already a registered user then login to AppHQ console and create an app from App Manager Tab.
  • Download the project from here & Unzip on your local system.
  • Open Constant.lua file in project folder of sample app and make the following changes:
  • Replace api-Key and secret-Key that you have received from AppHq.
  • Replace your data base name. ( Put and logical name and it will get created on the fly for your app)
  • To use PushNotification service in your application,create a new project in Google API Console.
  • Click on services option in Google developer console and enable Google Cloud Messaging for Android service.
  • Click on API Access tab and create a new server key for your application with blank server information.
  • Go to AppHQ console and click on Push Notification and select Android Settings in Settings option
  • Select your app and copy server key that is generated in Google developer console in above step and submit it.
  • Replace your project number in Config.lua.
  • Giving permission to your android device by putting the code in your build setting of corona project :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
settings = {
orientation = {
default = "portrait",
supported = { "portrait", }
},
android =
{
permissions =
{
{ name = ".permission.C2D_MESSAGE", protectionLevel = "signature" },
},
usesPermissions =
{
"android.permission.INTERNET",
"android.permission.GET_ACCOUNTS",
"android.permission.RECEIVE_BOOT_COMPLETED",
"com.google.android.c2dm.permission.RECEIVE",
".permission.C2D_MESSAGE",
},
},
}
  • Build and Run the application on Android

Application will contain the User registration and that user will also be registered for push. You can send a game challenge to your friend by clicking the challenge button. This will create the game for you and add opponent in game list behind the scene. After submission of your turn your opponent will get the push message for his turn.

You can Test and verify Push Notification separately  from AppHQ console as stated below:

  • After registering for Push Notification go to AppHQ console and click on Push Notification and select application after selecting User tab.
  • There is the list of user which has been registered for your app.

Design Details :

Initialization and Registration :

User Registration & Authentication

For user registration and authentication we have used create user and authenticate api .

Once your app has been successfully register with GCM or received message from user you can look into it by putting the onNotification listener in your app.

1
2
3
4
5
6
7
local function onNotification( event )
if event.type == "remoteRegistration" then
native.showAlert( "Register Your device with App42 here ", event.token, { "OK" } )
elseif event.type == "remote" then
native.showAlert( "On Notification", event.alert, { "OK" } )
end
end

Register your device with App42 for Push Messages :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
local userName = "Nick"
local deviceToken = ""
local deviceType = DeviceType.ANDROID
local App42CallBack = {}
pushService:storeDeviceToken(userName,deviceToken,deviceType,App42CallBack)
function App42CallBack:onSuccess(object)
--- Handle for response success
end
function App42CallBack:onException(exception)
print("Message is : "..exception:getMessage())
print("App Error code is : "..exception:getAppErrorCode())
print("Http Error code is "..exception:getHttpErrorCode())
print("Detail is : "..exception:getDetails())
end

Sending Push Message :

For push message we have user send push message to user api :

Push Notification Corona Making Turn based Game using App42 Corona SDK

1
2
3
4
5
6
7
8
9
10
11
12
13
local userName = "Nick"
local message= ""
local App42CallBack = {}
pushNotificationService:sendPushMessageToUser(userName,message,App42CallBack )
function App42CallBack:onSuccess(object)
--- Handle for response success
end
function App42CallBack:onException(exception)
print("Message is : "..exception:getMessage())
print("App Error code is : "..exception:getAppErrorCode())
print("Http Error code is "..exception:getHttpErrorCode())
print("Detail is : "..exception:getDetails())
end

If you have any questions or need any further assistance, please feel free to write us at support@shephertz.com.

twitter Making Turn based Game using App42 Corona SDKfacebook Making Turn based Game using App42 Corona SDKgoogle Making Turn based Game using App42 Corona SDKlinkedin Making Turn based Game using App42 Corona SDKpinterest Making Turn based Game using App42 Corona SDKreddit Making Turn based Game using App42 Corona SDKstumbleupon Making Turn based Game using App42 Corona SDKtumblr Making Turn based Game using App42 Corona SDK

Leave a Reply

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


− four = 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>