ok using those same 2 images...
// Project: makingaplayerwithtypes
// Created: 2014-11-25
// set window properties
SetWindowTitle( "multi player testing" )
// set display properties
SetVirtualResolution ( 640, 480 )
addvirtualjoystick(1,100,400,100)
global playerid //1=host 2=client
global networkid=0
global playerscount=0
global grabpersync=0 //should not send messages every sync....very bad if do
//hold player 2s position
global px2#
global py2#
//let player choose host or join
playerid=choosehostorjoin()
loadimage(1,"1.png",1)
loadimage(2,"2.png",1)
//setup host game
if playerid=1
networkid=hostnetwork("mygame", "player1", 1025)
createsprite(1,1)
setspriteposition(1,100,100)
setspritedepth(1,5)
createsprite(2,2)
setspriteposition(2,540,100)
setspritedepth(2,5)
endif
//setup client
if playerid=2
networkid=joinnetwork( "mygame","Player2")
createsprite(1,2)
setspriteposition(1,540,100)
setspritedepth(1,5)
createsprite(2,1)
setspriteposition(2,100,100)
setspritedepth(2,5)
endif
//these sprites are way too big...lets scale them down to 1/4th size
setspritescale(1,0.25,0.25)
setspritescale(2,0.25,0.25)
do
grabpersync=grabpersync+1
if grabpersync=>4 then grabpersync=0
//send a net message 15 times a second only
//if you send netmessages more than this....good luck not overloading your network
playerscount=GetNetworkNumClients( networkid )
if playerscount>1 then SetNetworkNoMoreClients( networkid) //this makes it so no more clients join
if playerid=1 then print(" Hosting game... ["+str(playerscount)+"] Players In Game")
if playerid=2 and playerscount=0 then print("looking to join game...")
if playerid=2 and playerscount=2 then print("Joined Game...")
if playerid=2 and playerscount=1 then print("Game Connection Lost...")
//update player
px#=getvirtualjoystickx(1)*2
py#=getvirtualjoysticky(1)*2
setspriteposition(1,getspritex(1)+px#,getspritey(1)+py#)
//check for net messages
netmessage = GetNetworkMessage ( networkid )
// cycle through all messages
while netmessage <> 0
// get command
cmd = GetNetworkMessageinteger ( netmessage )
//process command
if cmd=1
//other player asked for data
newmessage2=createnetworkmessage()
addnetworkmessageinteger(newmessage2,2) //cmd 2 is other player data
addnetworkmessagefloat(newmessage2,getspritex(1))
addnetworkmessagefloat(newmessage2,getspritey(1))
sendnetworkmessage(networkid,0,newmessage2)
endif
if cmd=2
//recieve other players data
px2#=getnetworkmessagefloat(netmessage)
py2#=getnetworkmessagefloat(netmessage)
//setspriteposition(2,px#,py#)
endif
// find the next message
deletenetworkmessage(netmessage)
//grab next message if any
netmessage = GetNetworkMessage ( networkid)
endwhile
//send and ask for other player data
if playerscount=2 and grabpersync=0
//ask client for data of other player
netmessage=CreateNetworkMessage()
addnetworkmessageinteger(netmessage,1) //cmd 1 ask for player data
sendnetworkmessage(networkid,0,netmessage)
endif
//no player 2??
if playerscount<>2
px2#=getspritex(2)
py2#=getspritey(2)
endif
//smoothen out player 2
setspriteposition(2,curvevalue#(getspritex(2),px2#,4),curvevalue#(getspritey(2),py2#,4))
sync()
loop
function choosehostorjoin()
if getvirtualbuttonexists(1)=1 then deletevirtualbutton(1)
if getvirtualbuttonexists(2)=1 then deletevirtualbutton(2)
addvirtualbutton(1,200,250,100)
addvirtualbutton(2,440,250,100)
setvirtualbuttontext(1,"Host")
setvirtualbuttontext(2,"Join")
choice=0
while choice=0
if getvirtualbuttonreleased(1)=1 then choice=1
if getvirtualbuttonreleased(2)=1 then choice=2
sync()
endwhile
deletevirtualbutton(1)
deletevirtualbutton(2)
sync()
endfunction choice
function curvevalue#(a#,b#,division)
if a#>b#
c#=a#-((a#-b#)/division)
exitfunction c#
endif
if b#>a#
c#=a#+((b#-a#)/division)
exitfunction c#
endif
endfunction a#
what i recommend doing is coding something to smoothen out both players so they are closer to looking the same across both machines over your local network.
what i tested this with was windows and blackberry playbook.
connected devices using wifi... have a fair bit of lag. so you
need to program in a way that you compensate or estimate where a player is headed.
currently its sending 15 messages a second... thats as smooth as i would go. You can test writting every sync but your network will lag something bad...even more if your using wifi.