Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Making a Turn-Based Game Using the App42 Corona SDK

DZone's Guide to

Making a Turn-Based Game Using the App42 Corona SDK

Learn how to build a turn-based game in your mobile app using the Corona SDK on the Android platform. Get the step-by-step guide here.

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

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 a turn-based, asynchronous game. We developed a sample turn-based Tic-Tac-Toe game using our Corona SDK for the Android platform, which can be used as a prototype for writing other turn-based games. This sample uses Push Notifications/Storage and User Service to implement the turn-based game. Here are few and easy steps to get started with this sample:

Running the Sample App

  • Register with the App42 platform.
  • Once you are on Getting Started page after registration, create an app.
  • If you are already a registered user, then login to AppHQ console and create an app from the App Manager Tab.
  • Download the project from here and unzip it on your local system.
  • Open the Constant.lua file in the project folder of the sample app and make the following changes:
    • Replace the API & Secret Keys from your App in AppHQ settings.
    • Replace your data base name (put a logical name and it will be created on the fly for your app).
  • To use the PushNotification service in your application, create a new project in Google API Console.
  • Click on the Services option in Google Developer Console and enable the Google Cloud Messaging for Android service.
  • Click on the API Access tab and create a new server key for your application with blank server information.
  • Go to the AppHQ console, click on Push Notification, and select Android Settings in the Settings option.
  • Select your app and copy the server key that was generated in Google Developer Console in the above step and submit it.
  • Replace your project number in Config.lua.
  • Give permission to your Android device by putting this code in your build setting of the Corona project:
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 an Android device.

The application will contain the user registration, and that user will also be registered for push notifications. You can send a game challenge to your friend by clicking the challenge button. This will create the game for you and add an opponent in the game list behind the scenes. After the submission of your turn, your opponent will get the push message for their turn.

You can test and verify push notifications separately from the AppHQ console as stated below:

  • After registering for push notifications, go to the AppHQ console, click on Push Notification, and select "application" in the 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

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 a message from the user, you can look into it by putting the onNotification listener in your app.

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:

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 Messages:

For push messages, we have the user send a push message to the user API:

Image title

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

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:
game dev ,game development ,corona ,mobile ,mobile app development

Published at DZone with permission of Himanshu Sharma. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}