1 / 6

Make realtime games with AppWarp and Corona SDK

This post will walk you through the integration of AppWarp Lua SDK with a simple Corona application which allows user to move objects in realtime across multiple clients.

shephertz
Download Presentation

Make realtime games with AppWarp and Corona SDK

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Make realtime games with AppWarp and Corona SDK In this post, I will walk you through the integration of AppWarp Lua SDK with a simple Corona application which allows user to move objects in realtime across multiple clients. It is based on the standard Drag sample that comes with Corona SDK. 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

  2. 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 HighlightsWe 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 )

  3. -- assign ids to buttons and insert in table button.id = tostring(item.id) myButtons[button.id] = button end We begin by establishing a connection with the server with a unique name. We then join and subscribe the game room. Once it’s 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 up first 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 there are any, 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

  4. 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.luaWe 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

  5. 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 that 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

  6. 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 update Peers. Original Blog Source: https://developer.coronalabs.com/code/multiplayer-game-development- using-appwarp-cloud

More Related