我试图将我的英雄在多人游戏设置中向左和向右移动。 我必须在正方形上开始向左移动并向右移动以在地面上进行常规的圆周运动。
我正在使用AppWarp来启动多人游戏实例并且工作正常,但是我在如何传达圆圈移动方面遇到了麻烦。
这是我的lua:
-- When left arrow is touched, move character left
function left:touch()
motionx = -speed;
end
left:addEventListener("touch",left)
-- When right arrow is touched, move character right
function right:touch()
motionx = speed;
end
right:addEventListener("touch",right)
-- Move character
local function movePlayer (event)
appWarpClient.sendUpdatePeers(tostring(player.x))
end
Runtime:addEventListener("enterFrame", movePlayer)
-- Stop character movement when no arrow is pushed
local function stop (event)
if event.phase =="ended" then
motionx = 0;
end
这里的keyline是
appWarpClient.sendUpdatePeers(tostring(player.x))
用这个我发送我的英雄(圆圈)的当前X位置,在我的warplisteners中,我这样选择它:
function onUpdatePeersReceived(update)
local func = string.gmatch(update, "%S+")
-- extract the sent values which are space delimited
--local id = tostring(func())
local x = func()
statusText.text = x
player.x = x + motionx
end
当我在2个客户端开始游戏时,我可以开始移动球但是它在62个单位之间来回摆动,我想我的速度是
speed = 6; -- Set Walking Speed
如果我改变了这个:
function onUpdatePeersReceived(update)
local func = string.gmatch(update, "%S+")
-- extract the sent values which are space delimited
--local id = tostring(func())
local x = func()
statusText.text = x
player.x = x + motionx
end
到此:
function onUpdatePeersReceived(update)
local func = string.gmatch(update, "%S+")
-- extract the sent values which are space delimited
--local id = tostring(func())
local x = func()
statusText.text = x
player.x = player.x + motionx
end
(“结束”之前的最后一行)
player.x = player.x + motionx
而不是
player.x = x + motionx
坐标会更新,但英雄只在其中一个屏幕上移动。
任何想法如何实现一个更好的移动系统,同时在两个客户端移动我的英雄?
亲切的问候
编辑:
添加if-else,它在+ -speed和0之间挂起,因为当英雄停止时速度为0。
if(motionx ~= "0") then
player.x = player.x + motionx
statusText.text = "motionx ~= 0"
elseif(motionx == "0") then
player.x = player.x
else
statusText.text ="Something went horribly wrong"
end