I'm trying to send a SKAction between devices but I've got some problem... could you help me please? This is my code to send it:
let movment1 = SKAction.moveTo(x: 1600, duration: 0.25)
car.run(movment1)
var positionToSend = movment1
let dataSend = Data(bytes: &positionToSend, count: MemoryLayout.size(ofValue: positionToSend))
try match?.sendData(toAllPlayers: dataSend, with: GKMatchSendDataMode.reliable)
and this is how I receive it:
func receiveDataP(position: Data, player: GKPlayer) {
let _ : SKAction = position.withUnsafeBytes { $0.pointee }
}
This is not working, in fact I was just trying...can someone help me rewriting the correct code? I'm getting mad...
Instead of sending the whole SKAction
, you can just send the necessary information needed to recreate the SKAction
in the other device.
Here, I suppose an SKAction
is described an x
value and a duration
. If duration
is always 0.25 then you don't need to send the duration.
Create something like this:
struct SKActionDescriptor : Codable {
let x: CGFloat
let duration: Double
}
To create the data to send, use a JSONEncoder
:
let encoder = JSONEncoder()
let data = try! encoder.encode(SKActionDescriptor(x: 1600, duration: 0.25))
And you can use a JSONDecoder
at the other end:
let decoder = JSONDecoder()
let descriptor = try! decoder.decode(SKActionDescriptor.self, from: receivedData)
And then create an SKAction
:
let action = SKAction.moveTo(x: descriptor.x, duration: descriptor.duration)