0

我正在尝试将我的英雄在多人游戏设置中左右移动。我必须方格开始向左移动并向右移动以在地面上进行常规圆圈。

我正在使用 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

这里的关键是

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

(“结束”之前的最后一行)

玩家.x = 玩家.x + 动作x

代替

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
4

0 回答 0