AppWarp game test bug? help!

-- -- Abstract: Move Vectors in realtime. Lets users drag update vector positions -- and updates the new positions on all clients. -- -- create global warp client and initialize it appWarpClient = require "AppWarp.WarpClient" -- Replace these with the values from AppHQ dashboard of your AppWarp app API\_KEY = "secret" SECRET\_KEY = "secret" STATIC\_ROOM\_ID = "secret" appWarpClient.initialize(API\_KEY, SECRET\_KEY) -- uncomment if you want to enable trace printing of appwarp --appWarpClient.enableTrace(true) -- IMPORTANT! loop WarpClient. This is required for receiving responses and notifications local function gameLoop(event) appWarpClient.Loop() end Runtime:addEventListener("enterFrame", gameLoop) -- do the appwarp client related handling in a separate file require "warplisteners" statusText = display.newText( "Connecting..", 10, display.contentHeight, native.systemFontBold, 24 ) statusText.width = 128 appWarpClient.connectWithUserName(tostring(os.clock())) button = {} -- Iterate through arguments array and create rounded rects (vector objects) for each item for i = 1, 2 do local button1 = display.newRoundedRect( 50\*i\*2, 60, 100, 100, 10 ) button1.strokeWidth = 6 button1:setStrokeColor( 200,200,200,255 ) button[i] = button1 end local function onTouch( event ) local t = button[1] local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) t.isFocus = true -- Store initial position t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then -- Make object move (we subtract t.x0,t.y0 so that moves are -- relative to initial grab point, rather than object "snapping"). t.x = event.x - t.x0 t.y = event.y - t.y0 appWarpClient.sendUpdatePeers(tostring(t.y)) elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false -- send the update to others in the game room. space delimit the values and parse accordingly -- in onUpdatePeersReceived notification appWarpClient.sendUpdatePeers(tostring(t.y)) end end return true end button[1]:addEventListener( "touch", onTouch ) 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 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 function onUpdatePeersReceived(update) local func = string.gmatch(update, "%S+") local y = func() local x = func() local buttonasd = button[2] buttonasd.y = tonumber(y) end appWarpClient.addRequestListener("onConnectDone", onConnectDone) appWarpClient.addRequestListener("onJoinRoomDone", onJoinRoomDone) appWarpClient.addRequestListener("onSubscribeRoomDone", onSubscribeRoomDone) appWarpClient.addNotificationListener("onUpdatePeersReceived", onUpdatePeersReceived) 

here is a link to video that shows the bug

https://www.youtube.com/watch?v=UJ41buMjXJs&feature=youtu.be

Soo, if we come from left to right we can call them boxes 1, 2, 3 and 4. So see in the video, when I move box 3 it moves box 2 AND also box 4. What I want it to do is when I move box 3 it only moves box 2 and not box4.

Same thing happens other way; when I move box 1 it moves box 2 and box 4.

So if anyone of you could please help me with this problem as soon as possible.

What looks like is happening here relates to these lines of code:

 local buttonasd = button[2] buttonasd.y = tonumber(y) 

Regardless of which button, you’re always moving the second button in the button[] array.

The solution is to pass additional information to the server so you can know which button to use. First you will need to give each button an ID that you can use:

for i = 1, 2 do local button1 = display.newRoundedRect( 50\*i\*2, 60, 100, 100, 10 ) button1.strokeWidth = 6 button1:setStrokeColor( 200,200,200,255 ) button1.name = i --\<----- add this line button[i] = button1 end

Now that each button as an id/name, in your touch handler you can provide more data to the server for use:

 t.x = event.x - t.x0 t.y = event.y - t.y0 local move = { x = t.x, y = t.y, id = t.name } appWarpClient.sendUpdatePeers(json.encode( move ))

This is of course assuming if you touch button one on one screen you want button one to move on the other. There are a couple of other bugs that will need to be fixed. First, since we are going to use JSON to make a string out of the table, you will need to do:

local json = require("json")

near the top of the project and then inside your touch handler you have this:

local function onTouch( event ) local t = button[1]

This means even if you touch button[2], your acting on button[1]'s data. I would change this to:

local function onTouch( event ) local t = event.target --\<----- the event table has a reference to the touched object

Finally I would move this whole block:

-- Iterate through arguments array and create rounded rects (vector objects) for each item for i = 1, 2 do local button1 = display.newRoundedRect( 50\*i\*2, 60, 100, 100, 10 ) button1.strokeWidth = 6 button1:setStrokeColor( 200,200,200,255 ) button[i] = button1 end

below the touch handling function then move the adding of the event listener inside the loop:

-- Iterate through arguments array and create rounded rects (vector objects) for each item for i = 1, 2 do local button1 = display.newRoundedRect( 50\*i\*2, 60, 100, 100, 10 ) button1.strokeWidth = 6 button1:setStrokeColor( 200,200,200,255 ) button1.name = i button[i] = button1 button[i]:addEventListener( "touch", onTouch ) end

And one final thought. You probably don’t want player 1 and player 2 moving the same buttons. You will need a way to identify each player. In the move table that you send, add another entry for playerID so you know who the move is coming from and either don’t set up the touch handler on each button, but only for the button of the current player.  Maybe:

-- Iterate through arguments array and create rounded rects (vector objects) for each item for i = 1, 2 do local button1 = display.newRoundedRect( 50\*i\*2, 60, 100, 100, 10 ) button1.strokeWidth = 6 button1:setStrokeColor( 200,200,200,255 ) button1.name = i button[i] = button1 if playerID == i then button[i]:addEventListener( "touch", onTouch ) end end

You will have to figure out how to determine and set playerID.  

Rob

Ok, I tried to added the code you sent to me. It does not work still. Here is video showing the new bug:

https://www.youtube.com/watch?v=w9BmXEi9N9Q&feature=youtu.be

and here is my code:

-- -- Abstract: Move Vectors in realtime. Lets users drag update vector positions -- and updates the new positions on all clients. -- -- create global warp client and initialize it appWarpClient = require "AppWarp.WarpClient" -- Replace these with the values from AppHQ dashboard of your AppWarp app API\_KEY = "9e6a154640418fd08d5b356dabb70945f8855d2bb5b1bdca724f5fce9b13296a" SECRET\_KEY = "d6a01a0254507b7f32d1f076e7dbfdde87f9247b21fb8108d1a254e0a2720771" STATIC\_ROOM\_ID = "947447229" appWarpClient.initialize(API\_KEY, SECRET\_KEY) -- uncomment if you want to enable trace printing of appwarp --appWarpClient.enableTrace(true) -- IMPORTANT! loop WarpClient. This is required for receiving responses and notifications local function gameLoop(event) appWarpClient.Loop() end Runtime:addEventListener("enterFrame", gameLoop) -- do the appwarp client related handling in a separate file require "warplisteners" local json = require("json") statusText = display.newText( "Connecting..", 10, display.contentHeight, native.systemFontBold, 24 ) statusText.width = 128 appWarpClient.connectWithUserName(tostring(os.clock())) button = {} local function onTouch( event ) local t = event.target local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) t.isFocus = true -- Store initial position t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then -- Make object move (we subtract t.x0,t.y0 so that moves are -- relative to initial grab point, rather than object "snapping"). t.x = event.x - t.x0 t.y = event.y - t.y0 local move = { id = tonumber(t.id), x = t.x, y = t.y } appWarpClient.sendUpdatePeers(json.encode( move )) elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false -- send the update to others in the game room. space delimit the values and parse accordingly -- in onUpdatePeersReceived notification end end return true end -- Iterate through arguments array and create rounded rects (vector objects) for each item for i = 1, 2 do local button1 = display.newRoundedRect( 50\*i\*2, 60, 100, 100, 10 ) button1.strokeWidth = 6 button1:setStrokeColor( 200,200,200,255 ) button1.id = i button[i] = button1 button[i]:addEventListener( "touch", onTouch ) end 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 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 function onUpdatePeersReceived(update) local func = string.gmatch(update, "%S+") local y = func() local id = func() print(tonumber(id)) local buttonasd = button[2] buttonasd.y = tonumber(y) end appWarpClient.addRequestListener("onConnectDone", onConnectDone) appWarpClient.addRequestListener("onJoinRoomDone", onJoinRoomDone) appWarpClient.addRequestListener("onSubscribeRoomDone", onSubscribeRoomDone) appWarpClient.addNotificationListener("onUpdatePeersReceived", onUpdatePeersReceived) 

ok, so when I move button 2 it moves glitchy and when I try to print id in the last function it gives me nil, I have tried to print(tonumber(id)), tostring, totable…

Please help me if you can.

Also I could not change 

 local buttonasd = button[2]

to

 local buttonasd = button[id]

the below does not work, when I type that it gives me following error: ---------------------------

Corona Runtime Error


main.lua:111: attempt to index local ‘buttonasd’ (a nil value)

stack traceback:

main.lua:111: in function ‘onUpdatePeersReceived’

AppWarp\WarpClient.lua:71: in function ‘onNotify’

AppWarp\WarpClient.lua:357: in function ‘receivedData’

AppWarp\WarpChannel.lua:51: in function ‘socket_recv’

AppWarp\WarpClient.lua:463: in function ‘Loop’

main.lua:21: in function ‘?’

?: in function <?:182>

Do you want to relaunch the project?


Yes   No   


You need to update this function to handle the data you’re now passing:

function onUpdatePeersReceived(update) local func = string.gmatch(update, "%S+") local y = func() local id = func() print(tonumber(id)) local buttonasd = button[2] buttonasd.y = tonumber(y) end 

maybe something like:

function onUpdatePeersReceived(update) local move = json.decode(update) local id = move.id local buttonasd = button[id] buttonasd.y = move.y end 

I’ve not tested any of this, but give that a try.

Ok that kind of worked! But I have a new issue, here is my code:

-- -- Abstract: Move Vectors in realtime. Lets users drag update vector positions -- and updates the new positions on all clients. -- -- create global warp client and initialize it appWarpClient = require "AppWarp.WarpClient" -- Replace these with the values from AppHQ dashboard of your AppWarp app API\_KEY = "9e6a154640418fd08d5b356dabb70945f8855d2bb5b1bdca724f5fce9b13296a" SECRET\_KEY = "d6a01a0254507b7f32d1f076e7dbfdde87f9247b21fb8108d1a254e0a2720771" STATIC\_ROOM\_ID = "947447229" appWarpClient.initialize(API\_KEY, SECRET\_KEY) -- uncomment if you want to enable trace printing of appwarp --appWarpClient.enableTrace(true) -- IMPORTANT! loop WarpClient. This is required for receiving responses and notifications local function gameLoop(event) appWarpClient.Loop() end Runtime:addEventListener("enterFrame", gameLoop) -- do the appwarp client related handling in a separate file local json = require("json") local physics = require("physics") physics.start() physics.setGravity(0, 0) local x = display.contentCenterX local y = display.contentCenterY local w = display.actualContentWidth local h = display.contentHeight local applyForceX local startRequested = false -- this function creates some (if not all of them) visible/displayable things local function createObstacles() local wall1 = display.newRect(x + w / 2, y, 10, h) wall1:setFillColor(1,1,1) wall1.name = "wall1" physics.addBody(wall1, "static", {bounce = 1}) local wall2 = display.newRect(x - w / 2, y, 10, h) wall2:setFillColor(1,1,1) physics.addBody(wall2, "static", {bounce = 1}) wall2.name = "wall2" roof1 = display.newRect(x, y - h / 2, w, 10) roof1:setFillColor(1,1,1) physics.addBody(roof1, "static", {bounce = 1}) roof1.name = "roof1" roof2 = display.newRect(x, y + h / 2, w, 10) roof2:setFillColor(1,1,1) physics.addBody(roof2, "static", {bounce = 1}) roof2.name = "roof2" es = display.setDefault("background", 0, 0, 0) statusText = display.newText( "Connecting..", x, y + 30, native.systemFontBold, 10 ) statusText.width = 150 statusText.alpha = 0.1 startButton = display.newText("Start", x, y, native.systemFontBold, 24) startButton.alpha = 0.5 --scoreText = display.newText(""..scoreAI.." "..score.."", x, y - h/2.3, es, 25) --scoreText:setFillColor(1,1,1) --scoreText:toBack() end createObstacles() local playerID = tostring(os.clock()) appWarpClient.connectWithUserName(playerID) button = {} -- this function will start up the physics create ball and make it move and some other things function gameStart(asd) physics.start() if ball then physics.removeBody(ball) display.remove(ball) ball = nil end ball = display.newCircle(x, y, 10) ball.name = "ball" timer.performWithDelay(50, function() physics.addBody(ball, {bounce = 1, radius = 10}) ball:applyForce(2, applyForceX, ball.x, ball.y) end, 1) online = true end -- this function resets the game, stopping physics and removing ball and some other things -- also makes the screen flash several times before starting up the game again function gameReset() physics.stop() physics.removeBody(ball) display.remove(ball) ball = nil online = false end local function onTouch( event ) t = event.target local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) t.isFocus = true -- Store initial position t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then -- Make object move (we subtract t.x0,t.y0 so that moves are -- relative to initial grab point, rather than object "snapping"). t.y = event.y - t.y0 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false -- send the update to others in the game room. space delimit the values and parse accordingly -- in onUpdatePeersReceived notification end end return true end local function startGame(event) startButton:removeEventListener("touch", startGame) display.remove(event.target) if startRequested == true then applyForceX = math.random(-300,300) / 100 gameStart(true) local move = { peerUpdate = "start", playerID = playerID, gameStarted = online, applyForceX} appWarpClient.sendUpdatePeers(json.encode( move )) else local move = { peerUpdate = "requestStart", playerID = playerID, gameStarted = online } appWarpClient.sendUpdatePeers(json.encode( move )) end end local padx = {} padx[1] = x - w / 2.2 padx[2] = x + w / 2.2 -- Iterate through arguments array and create rounded rects (vector objects) for each item for i = 1, 2 do local pad1 = display.newRect(padx[i], y, 8, 100) pad1.strokeWidth = 6 pad1:setStrokeColor( 200,200,200,255 ) pad1.id = i physics.addBody(pad1, "static", {bounce = 1}) button[i] = pad1 button[1]:addEventListener( "touch", onTouch ) end 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 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 = "Connected!" startButton.alpha = 1 startButton:addEventListener("touch", startGame) else statusText.text = "Room Subscribe Failed" end end local function update() if t ~= nil then local move = { peerUpdate = "pad", id = tonumber(t.id), y = t.y, playerID = playerID, gameStarted = online } appWarpClient.sendUpdatePeers(json.encode( move )) end if ball ~= nil then local move = { peerUpdate = "ball", ballx = ball.x, bally = ball.y, playerID = playerID, gameStarted = online } appWarpClient.sendUpdatePeers(json.encode( move )) end end Runtime:addEventListener("enterFrame", update) function onUpdatePeersReceived(update) local move = json.decode(update) local id = move.id local buttonasd = button[2] if move.peerUpdate == "pad" then if playerID ~= move.playerID then buttonasd.y = move.y end elseif move.peerUpdate == "ball" then if ball ~= nil then ball.x = move.ballx ball.y = move.bally end elseif move.peerUpdate == "requestStart" and playerID ~= move.playerID then startRequested = true elseif move.peerUpdate == "start" then gameStart() end end appWarpClient.addRequestListener("onConnectDone", onConnectDone) appWarpClient.addRequestListener("onJoinRoomDone", onJoinRoomDone) appWarpClient.addRequestListener("onSubscribeRoomDone", onSubscribeRoomDone) appWarpClient.addNotificationListener("onUpdatePeersReceived", onUpdatePeersReceived) 

so, here is the video showing the problem
https://www.youtube.com/watch?v=EkNB4WdJ57E&feature=youtu.be

When I started this thread and had a problem I was creating a Pong game, BUT only testing the networking. Now that I have made the Pong core (sort of) it does not work too well. The problem is that the pong ball moves glitchy.

Yea, and also I have made this pong game so when I move the left pad of player1’s screen it moves the right pad of player2’s screen and when I move player2’s screen left pad it moves player1’s screen right pad, because for me it is the easiest to do, but as you can see in the video I tried to flip the ball by the y axis by multiplying the ball.x by -1 but it just started to get even buggier.

Btw. I was not sure if I should make this question here or make a new thread, but I made this here. Feel free to move my thread somewhere else, if I should not have posted this in here.

What looks like is happening here relates to these lines of code:

 local buttonasd = button[2] buttonasd.y = tonumber(y) 

Regardless of which button, you’re always moving the second button in the button[] array.

The solution is to pass additional information to the server so you can know which button to use. First you will need to give each button an ID that you can use:

for i = 1, 2 do local button1 = display.newRoundedRect( 50\*i\*2, 60, 100, 100, 10 ) button1.strokeWidth = 6 button1:setStrokeColor( 200,200,200,255 ) button1.name = i --\<----- add this line button[i] = button1 end

Now that each button as an id/name, in your touch handler you can provide more data to the server for use:

 t.x = event.x - t.x0 t.y = event.y - t.y0 local move = { x = t.x, y = t.y, id = t.name } appWarpClient.sendUpdatePeers(json.encode( move ))

This is of course assuming if you touch button one on one screen you want button one to move on the other. There are a couple of other bugs that will need to be fixed. First, since we are going to use JSON to make a string out of the table, you will need to do:

local json = require("json")

near the top of the project and then inside your touch handler you have this:

local function onTouch( event ) local t = button[1]

This means even if you touch button[2], your acting on button[1]'s data. I would change this to:

local function onTouch( event ) local t = event.target --\<----- the event table has a reference to the touched object

Finally I would move this whole block:

-- Iterate through arguments array and create rounded rects (vector objects) for each item for i = 1, 2 do local button1 = display.newRoundedRect( 50\*i\*2, 60, 100, 100, 10 ) button1.strokeWidth = 6 button1:setStrokeColor( 200,200,200,255 ) button[i] = button1 end

below the touch handling function then move the adding of the event listener inside the loop:

-- Iterate through arguments array and create rounded rects (vector objects) for each item for i = 1, 2 do local button1 = display.newRoundedRect( 50\*i\*2, 60, 100, 100, 10 ) button1.strokeWidth = 6 button1:setStrokeColor( 200,200,200,255 ) button1.name = i button[i] = button1 button[i]:addEventListener( "touch", onTouch ) end

And one final thought. You probably don’t want player 1 and player 2 moving the same buttons. You will need a way to identify each player. In the move table that you send, add another entry for playerID so you know who the move is coming from and either don’t set up the touch handler on each button, but only for the button of the current player.  Maybe:

-- Iterate through arguments array and create rounded rects (vector objects) for each item for i = 1, 2 do local button1 = display.newRoundedRect( 50\*i\*2, 60, 100, 100, 10 ) button1.strokeWidth = 6 button1:setStrokeColor( 200,200,200,255 ) button1.name = i button[i] = button1 if playerID == i then button[i]:addEventListener( "touch", onTouch ) end end

You will have to figure out how to determine and set playerID.  

Rob

Ok, I tried to added the code you sent to me. It does not work still. Here is video showing the new bug:

https://www.youtube.com/watch?v=w9BmXEi9N9Q&feature=youtu.be

and here is my code:

-- -- Abstract: Move Vectors in realtime. Lets users drag update vector positions -- and updates the new positions on all clients. -- -- create global warp client and initialize it appWarpClient = require "AppWarp.WarpClient" -- Replace these with the values from AppHQ dashboard of your AppWarp app API\_KEY = "9e6a154640418fd08d5b356dabb70945f8855d2bb5b1bdca724f5fce9b13296a" SECRET\_KEY = "d6a01a0254507b7f32d1f076e7dbfdde87f9247b21fb8108d1a254e0a2720771" STATIC\_ROOM\_ID = "947447229" appWarpClient.initialize(API\_KEY, SECRET\_KEY) -- uncomment if you want to enable trace printing of appwarp --appWarpClient.enableTrace(true) -- IMPORTANT! loop WarpClient. This is required for receiving responses and notifications local function gameLoop(event) appWarpClient.Loop() end Runtime:addEventListener("enterFrame", gameLoop) -- do the appwarp client related handling in a separate file require "warplisteners" local json = require("json") statusText = display.newText( "Connecting..", 10, display.contentHeight, native.systemFontBold, 24 ) statusText.width = 128 appWarpClient.connectWithUserName(tostring(os.clock())) button = {} local function onTouch( event ) local t = event.target local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) t.isFocus = true -- Store initial position t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then -- Make object move (we subtract t.x0,t.y0 so that moves are -- relative to initial grab point, rather than object "snapping"). t.x = event.x - t.x0 t.y = event.y - t.y0 local move = { id = tonumber(t.id), x = t.x, y = t.y } appWarpClient.sendUpdatePeers(json.encode( move )) elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false -- send the update to others in the game room. space delimit the values and parse accordingly -- in onUpdatePeersReceived notification end end return true end -- Iterate through arguments array and create rounded rects (vector objects) for each item for i = 1, 2 do local button1 = display.newRoundedRect( 50\*i\*2, 60, 100, 100, 10 ) button1.strokeWidth = 6 button1:setStrokeColor( 200,200,200,255 ) button1.id = i button[i] = button1 button[i]:addEventListener( "touch", onTouch ) end 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 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 function onUpdatePeersReceived(update) local func = string.gmatch(update, "%S+") local y = func() local id = func() print(tonumber(id)) local buttonasd = button[2] buttonasd.y = tonumber(y) end appWarpClient.addRequestListener("onConnectDone", onConnectDone) appWarpClient.addRequestListener("onJoinRoomDone", onJoinRoomDone) appWarpClient.addRequestListener("onSubscribeRoomDone", onSubscribeRoomDone) appWarpClient.addNotificationListener("onUpdatePeersReceived", onUpdatePeersReceived) 

ok, so when I move button 2 it moves glitchy and when I try to print id in the last function it gives me nil, I have tried to print(tonumber(id)), tostring, totable…

Please help me if you can.

Also I could not change 

 local buttonasd = button[2]

to

 local buttonasd = button[id]

the below does not work, when I type that it gives me following error: ---------------------------

Corona Runtime Error


main.lua:111: attempt to index local ‘buttonasd’ (a nil value)

stack traceback:

main.lua:111: in function ‘onUpdatePeersReceived’

AppWarp\WarpClient.lua:71: in function ‘onNotify’

AppWarp\WarpClient.lua:357: in function ‘receivedData’

AppWarp\WarpChannel.lua:51: in function ‘socket_recv’

AppWarp\WarpClient.lua:463: in function ‘Loop’

main.lua:21: in function ‘?’

?: in function <?:182>

Do you want to relaunch the project?


Yes   No   


You need to update this function to handle the data you’re now passing:

function onUpdatePeersReceived(update) local func = string.gmatch(update, "%S+") local y = func() local id = func() print(tonumber(id)) local buttonasd = button[2] buttonasd.y = tonumber(y) end 

maybe something like:

function onUpdatePeersReceived(update) local move = json.decode(update) local id = move.id local buttonasd = button[id] buttonasd.y = move.y end 

I’ve not tested any of this, but give that a try.

Ok that kind of worked! But I have a new issue, here is my code:

-- -- Abstract: Move Vectors in realtime. Lets users drag update vector positions -- and updates the new positions on all clients. -- -- create global warp client and initialize it appWarpClient = require "AppWarp.WarpClient" -- Replace these with the values from AppHQ dashboard of your AppWarp app API\_KEY = "9e6a154640418fd08d5b356dabb70945f8855d2bb5b1bdca724f5fce9b13296a" SECRET\_KEY = "d6a01a0254507b7f32d1f076e7dbfdde87f9247b21fb8108d1a254e0a2720771" STATIC\_ROOM\_ID = "947447229" appWarpClient.initialize(API\_KEY, SECRET\_KEY) -- uncomment if you want to enable trace printing of appwarp --appWarpClient.enableTrace(true) -- IMPORTANT! loop WarpClient. This is required for receiving responses and notifications local function gameLoop(event) appWarpClient.Loop() end Runtime:addEventListener("enterFrame", gameLoop) -- do the appwarp client related handling in a separate file local json = require("json") local physics = require("physics") physics.start() physics.setGravity(0, 0) local x = display.contentCenterX local y = display.contentCenterY local w = display.actualContentWidth local h = display.contentHeight local applyForceX local startRequested = false -- this function creates some (if not all of them) visible/displayable things local function createObstacles() local wall1 = display.newRect(x + w / 2, y, 10, h) wall1:setFillColor(1,1,1) wall1.name = "wall1" physics.addBody(wall1, "static", {bounce = 1}) local wall2 = display.newRect(x - w / 2, y, 10, h) wall2:setFillColor(1,1,1) physics.addBody(wall2, "static", {bounce = 1}) wall2.name = "wall2" roof1 = display.newRect(x, y - h / 2, w, 10) roof1:setFillColor(1,1,1) physics.addBody(roof1, "static", {bounce = 1}) roof1.name = "roof1" roof2 = display.newRect(x, y + h / 2, w, 10) roof2:setFillColor(1,1,1) physics.addBody(roof2, "static", {bounce = 1}) roof2.name = "roof2" es = display.setDefault("background", 0, 0, 0) statusText = display.newText( "Connecting..", x, y + 30, native.systemFontBold, 10 ) statusText.width = 150 statusText.alpha = 0.1 startButton = display.newText("Start", x, y, native.systemFontBold, 24) startButton.alpha = 0.5 --scoreText = display.newText(""..scoreAI.." "..score.."", x, y - h/2.3, es, 25) --scoreText:setFillColor(1,1,1) --scoreText:toBack() end createObstacles() local playerID = tostring(os.clock()) appWarpClient.connectWithUserName(playerID) button = {} -- this function will start up the physics create ball and make it move and some other things function gameStart(asd) physics.start() if ball then physics.removeBody(ball) display.remove(ball) ball = nil end ball = display.newCircle(x, y, 10) ball.name = "ball" timer.performWithDelay(50, function() physics.addBody(ball, {bounce = 1, radius = 10}) ball:applyForce(2, applyForceX, ball.x, ball.y) end, 1) online = true end -- this function resets the game, stopping physics and removing ball and some other things -- also makes the screen flash several times before starting up the game again function gameReset() physics.stop() physics.removeBody(ball) display.remove(ball) ball = nil online = false end local function onTouch( event ) t = event.target local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) t.isFocus = true -- Store initial position t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then -- Make object move (we subtract t.x0,t.y0 so that moves are -- relative to initial grab point, rather than object "snapping"). t.y = event.y - t.y0 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false -- send the update to others in the game room. space delimit the values and parse accordingly -- in onUpdatePeersReceived notification end end return true end local function startGame(event) startButton:removeEventListener("touch", startGame) display.remove(event.target) if startRequested == true then applyForceX = math.random(-300,300) / 100 gameStart(true) local move = { peerUpdate = "start", playerID = playerID, gameStarted = online, applyForceX} appWarpClient.sendUpdatePeers(json.encode( move )) else local move = { peerUpdate = "requestStart", playerID = playerID, gameStarted = online } appWarpClient.sendUpdatePeers(json.encode( move )) end end local padx = {} padx[1] = x - w / 2.2 padx[2] = x + w / 2.2 -- Iterate through arguments array and create rounded rects (vector objects) for each item for i = 1, 2 do local pad1 = display.newRect(padx[i], y, 8, 100) pad1.strokeWidth = 6 pad1:setStrokeColor( 200,200,200,255 ) pad1.id = i physics.addBody(pad1, "static", {bounce = 1}) button[i] = pad1 button[1]:addEventListener( "touch", onTouch ) end 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 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 = "Connected!" startButton.alpha = 1 startButton:addEventListener("touch", startGame) else statusText.text = "Room Subscribe Failed" end end local function update() if t ~= nil then local move = { peerUpdate = "pad", id = tonumber(t.id), y = t.y, playerID = playerID, gameStarted = online } appWarpClient.sendUpdatePeers(json.encode( move )) end if ball ~= nil then local move = { peerUpdate = "ball", ballx = ball.x, bally = ball.y, playerID = playerID, gameStarted = online } appWarpClient.sendUpdatePeers(json.encode( move )) end end Runtime:addEventListener("enterFrame", update) function onUpdatePeersReceived(update) local move = json.decode(update) local id = move.id local buttonasd = button[2] if move.peerUpdate == "pad" then if playerID ~= move.playerID then buttonasd.y = move.y end elseif move.peerUpdate == "ball" then if ball ~= nil then ball.x = move.ballx ball.y = move.bally end elseif move.peerUpdate == "requestStart" and playerID ~= move.playerID then startRequested = true elseif move.peerUpdate == "start" then gameStart() end end appWarpClient.addRequestListener("onConnectDone", onConnectDone) appWarpClient.addRequestListener("onJoinRoomDone", onJoinRoomDone) appWarpClient.addRequestListener("onSubscribeRoomDone", onSubscribeRoomDone) appWarpClient.addNotificationListener("onUpdatePeersReceived", onUpdatePeersReceived) 

so, here is the video showing the problem
https://www.youtube.com/watch?v=EkNB4WdJ57E&feature=youtu.be

When I started this thread and had a problem I was creating a Pong game, BUT only testing the networking. Now that I have made the Pong core (sort of) it does not work too well. The problem is that the pong ball moves glitchy.

Yea, and also I have made this pong game so when I move the left pad of player1’s screen it moves the right pad of player2’s screen and when I move player2’s screen left pad it moves player1’s screen right pad, because for me it is the easiest to do, but as you can see in the video I tried to flip the ball by the y axis by multiplying the ball.x by -1 but it just started to get even buggier.

Btw. I was not sure if I should make this question here or make a new thread, but I made this here. Feel free to move my thread somewhere else, if I should not have posted this in here.