0

I am trying to move my hero in a multiplayer setup to the left and right. I have to squares to initiate move left and move right for a regular crircle on a ground.

I am using AppWarp to initate the multiplayer instance and that is working fine, however I am having trouble on how to communicate the moving of the circle.

Here is my 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

The keyline here is

appWarpClient.sendUpdatePeers(tostring(player.x))

with this I send the current X position of my hero (the circle) and in my warplisteners I pick it up like so:

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

When I start the game on 2 clients I can start moving the ball but it is wobbeling back and forth between 62 units, wich i guess is my speed

speed = 6; -- Set Walking Speed

if I change this:

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

to this:

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

(last line before "end")

player.x = player.x + motionx

instead of

player.x = x + motionx

The coordinates gets updated but the hero only moves on one of the screens.

Any idea how to implement a better movement system that moves my hero on both clients at the same time?

Kind regards

edit:

Added if-else, it was pending between +-speed and 0 since the speed is 0 when hero is stopped.

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
darrrrUC
  • 311
  • 2
  • 15

0 Answers0