Our App42 Corona SDK has been here for years and been trusted by thousands of game and app developers across the world. Our forum received a lot of queries 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 which 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 Getting Started 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 & Secret Keys from your App in AppHQ settings.
- Replace your data base name. ( Put a 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 :
[code java]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",
},
},
}[/code]
- Build and Run the application on an Android device
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 their 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 users that has been registered for your app.
Also Read: App42 Ebook on Developing Turn Based Games
Also Check: AppWarp Tutorial on Realtime Turn Based Games
Design Details :
Initialization and Registration :
User Registration & Authentication
For user registration and authentication we have used create user and authenticate APIs.
Once your app has been successfully registered with GCM or received message from user, you can look into it by putting the onNotification listener in your app.
[code java]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[/code]
Register your device with App42 for Push Messages :
[code java]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[/code]
Sending Push Message :
For push message we have user send push message to user API:
[code java]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[/code]
If you have any questions or need any further assistance, please feel free to write to us at support@shephertz.com.
Leave A Reply