corona-color-move-walkthrough

*We assure you that we do not spam. You may receive occasional emails from us.
 You can always unsubscribe.

Corona Color Move Walkthrough

In this wiki, we will walk through the integration of AppWarp Lua SDK with a simple Corona application which allows user to move objects in realtime across multiple client.

Project Setup and Running

  • Follow these steps and get your key pair and a room id.
  • Download the color move sample code from our git repo.
  • Copy the AppWarp folder from the latest version and put it in the Sample’s folder. It should look like this
  • Color Move Walkthrough Step1

  • Edit main.lua and replace the keys and roomid where indicated with the values you got from AppHq dashboard
  • Load the Sample in Corona Simulator and verify that it loads without any errors

Code Highlights
We first draw the objects (color vectors) on the screen and register for touch listeners. Users will be dragging these around. It is based on the DragMe sample available in the Corona SDK.

-- Iterate through arguments array and create rounded rects (vector objects) for each item
for _,item in ipairs( arguments ) do
	local button = display.newRoundedRect( item.x, item.y, item.w, item.h, item.r )
	button:setFillColor( item.red, item.green, item.blue )
	button.strokeWidth = 6
	button:setStrokeColor( 200,200,200,255 )
	-- Make the button instance respond to touch events
	button:addEventListener( "touch", onTouch )
  -- assign ids to buttons and insert in table
  button.id = tostring(item.id)
  myButtons[button.id] = button  
end

We begin my establishing a connection with the server with a unique name. We then join and subscribe the game room. Once done, users can drag and drop the objects and in realtime other users in the room can see the objects move.
main.lua

appWarpClient = require "AppWarp.WarpClient"
appWarpClient.initialize(API_KEY, SECRET_KEY)

We create a global instance of the appwarp client and initialize it. It is important to keep this instance global and access the same one in your interactions with appwarp. Initialization with your developer keys is important so that your communication with the server is authenticated. Hence it must be called first up and before you connect with the server.
This following snippet is important as it provides for the asynchronous communication with the server. It registers the “enterFrame” listener with the gameLoop callback. In this callback we call the Loop api of the appwarp client instance. This allows appwarp client to check if there are any pending messages from the server and if so notify the application.

local function gameLoop(event)
  appWarpClient.Loop()
end
Runtime:addEventListener("enterFrame", gameLoop) 

Once the setup is complete, we establish the connection with appwarp server. Concurrent connections to the same application in appwarp must have unique names. App developers are free to use any ID provider as long as it provides unique names such as facebook, google+ etc. or from their own user management system. In this case we simply use the time to get unique names.

-- start connecting with a random name
appWarpClient.connectWithUserName(tostring(os.clock()))

To receive callbacks from appwarp client, we need to register the corresponding listeners. The code for that is in warplisteners.lua
We register the listeners as follows

appWarpClient.addRequestListener("onConnectDone", onConnectDone)
appWarpClient.addRequestListener("onJoinRoomDone", onJoinRoomDone)
appWarpClient.addRequestListener("onSubscribeRoomDone", onSubscribeRoomDone)

And define the callbacks as follows

function onConnectDone(resultCode)
  if(resultCode == WarpResponseResultCode.SUCCESS) then
    statusText.text = "Joining Room.."
    appWarpClient.joinRoom(STATIC_ROOM_ID)
  elseif(resultCode == WarpResponseResultCode.AUTH_ERROR) then
    statusText.text = "Incorrect app keys"
  else
    statusText.text = "Connect Failed. Restart"
  end  
end

Upon success, we will join and subscribe the game room.

function onJoinRoomDone(resultCode)
  if(resultCode == WarpResponseResultCode.SUCCESS) then
    appWarpClient.subscribeRoom(STATIC_ROOM_ID)
    statusText.text = "Subscribing to room.."
  else
    statusText.text = "Room Join Failed"
  end  
end
function onSubscribeRoomDone(resultCode)
  if(resultCode == WarpResponseResultCode.SUCCESS) then    
    statusText.text = "Started!"
  else
    statusText.text = "Room Subscribe Failed"
  end  
end

We can now send updates to other players whenever the user drags and drops one of the pieces. We will use the update peers API for sending these updates. We send the object id and its x,y coordinates.

 appWarpClient.sendUpdatePeers(tostring(t.id) .. " " .. tostring(t.x).." ".. tostring(t.y))
 

To receive updates so we can update positions based on network events, we need to register for this notification and handle it.

appWarpClient.addNotificationListener("onUpdatePeersReceived", onUpdatePeersReceived)
 

We define the callback as follows. We will simply parse the received message according to how we send it i.e. the id and its x,y coordinates.

function onUpdatePeersReceived(update)
  local func = string.gmatch(update, "%S+")
  -- extract the sent values which are space delimited
  local id = tostring(func())
  local x = func()
  local y = func()
  local button = myButtons[id]
  button.x = tonumber(x)
  button.y = tonumber(y)
  print("someone moved button id ".. id)
end

That’s it. We walked through some of the key concepts of integrating AppWarp Lua SDK in your application. We illustrated how you register request and notification listeners. We illustrated the basic room operations of join/subscribe and updatePeers.