Question

Here's the situation: I am making a multiplayer version of pong for android. When my paddle connects with the ball, I want to send the other player the point of impact of the ball (as an x,y coordinate) so that the other player's screen can update to show where the ball is coming from as well as the position of my paddle. The other device will calculate the ball's trajectory and eventually repeat this process. Essentially what I want is something that looks like this--

static final int RESPONSE_CODE = (some #);

int[] return_data = {RESPONSE_CODE, myPaddle.x, myPaddle.y, ballImpactPosition.x, ballImpactPosition.y};

blueToothConnection.write(return_data);

The problem here is that bluetoothConnection.write() relies on java.io.OutputStream and so only supports writing a byte[].

So, is there any way to convert my int[] into a byte[] (and back again later)? If it helps, my int[] will always contain exactly 5 ints, all always positive. Im not sure what the max values of my ints are because that depends on the device's screen size.

Was it helpful?

Solution

In this case, the ByteBuffer is your friend:

Android API link for ByeBuffer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top