- Lua Socket
```
local socket = require("socket")
local widget= require ("widget")
local client= socket.connect("www.google.com", 80)
local ip, port = client:getsockname()
udp=socket.udp()
udp:settimeout(0)
udp:setsockname(ip, port)
local data
ipp = display.newText("IP = "..ip, display.contentCenterX, 80, native.systemFont, 60)
portt = display.newText("PORT = "..port, display.contentCenterX, 160, native.systemFont, 60)
sendIP=native.newTextField(display.contentCenterX, 250, 500, 80)
sendPORT=native.newTextField(display.contentCenterX, 350, 500, 80)
sendIP.text=ip
sendPORT.text=port
function ButEvent(e)
if (e.phase=="began") and (sendIP.text~="") then
udpp=socket.udp()
udpp:settimeout(0)
udpp:setpeername(sendIP.text, tonumber(sendPORT.text))
local dg = e.target.id
local snd = udpp:send(dg)
end
end
upBut=widget.newButton{
defaultFile = "buttonHelp.png",
left=display.contentCenterX-120,
top=display.contentCenterY-200,
width = 240,
height = 120,
label="Up",
id="up",
onEvent=ButEvent,
}
ball = widget.newButton{
left=100,
top=260,
width=100,
height=100,
defaultFile = "football.png",
id="ball",
}
downBut=widget.newButton{
defaultFile = "buttonHelp.png",
left=display.contentCenterX-120,
top=display.contentCenterY+200,
width = 240,
height = 120,
label="Down",
id="down",
onEvent=ButEvent,
}
leftBut=widget.newButton{
defaultFile = "buttonHelp.png",
left=display.contentCenterX-120*3,
top=display.contentCenterY,
width = 240,
height = 120,
label="left",
id="left",
onEvent=ButEvent,
}
rightBut=widget.newButton{
defaultFile = "buttonHelp.png",
left=display.contentCenterX+120,
top=display.contentCenterY,
width = 240,
height = 120,
label="Right",
id="right",
onEvent=ButEvent,
}
local function receiveUdpMsg(e)
print("R")
data = udp:receive()
if data ~= nil then
if (data=="up")then ball.y=ball.y-10
print("up")
elseif (data=="down") then ball.y=ball.y+10
print("Down")
elseif (data=="left") then ball.x=ball.x-10
print("left")
elseif (data=="right") then ball.x=ball.x+10
print("Right")
end
end
socket.sleep(0.01)
recv= timer.performWithDelay(100, receiveUdpMsg)
end
recv= timer.performWithDelay(100, receiveUdpMsg)
```
References
- https://coronalabs.com/blog/2014/09/23/tutorial-local-multiplayer-with-udptcp/
Tutorial: Local multiplayer with UDP/TCP | Corona Labs
Today's guest tutorial comes to you courtesy of Mark Steelman, founder of Steelman Games LLC. Mark is currently working on a turn-based RPG incorporating local multiplayer called Legend of Us Roleplaying Game. Before becoming an indie developer, Mark worke
coronalabs.com
- 참고 동영상
References
http://underpop.online.fr/l/lua/luasocket/
LuaSocket: Network support for the Lua language
What is LuaSocket? LuaSocket is a Lua extension library that is composed by two parts: a C core that provides support for the TCP and UDP transport layers, and a set of Lua modules that add support for functionality commonly needed by applications that dea
underpop.online.fr
'tech > game' 카테고리의 다른 글
21/07/14 (Brian Burton) Game Development with Corona (0) | 2021.07.14 |
---|---|
21/07/14 Mobile Game Development with Corona SDK (Solar2D) (0) | 2021.07.14 |
21/07/13 How to RUN a LIVE BUILD TEST of your APP using SOLAR2D (0) | 2021.07.14 |
21/07/13 TicTacToe - MUTE SOUND EFFECTS (USING EXTERNAL MODULES) (0) | 2021.07.14 |
21/07/13 TicTacToe - SOUND EFFECTS (0) | 2021.07.14 |