我正在尝试开发multiplayer
android
游戏。要在同级之间发送消息,我使用的是sendReliableMessage
中的mRealTimeMultiplayerClient
。我想分享的消息是我收到的随机信件。问题在于我在发送邮件之前收到的信件与收到邮件之后收到的信件不同,而且我不知道我的错误在哪里。
在此方法中,我调用该方法来随机化字母并通过mRealTimeMultiplayerClient
byte[] mLetterBuf = new byte[1];
void broadcastLetter(){
if (!mMultiplayer) {
// playing single-player mode
return;
}
mLetterBuf = randomLetter();
//mLetterBuf[0] = mLetter.getBytes()[0];
// Send to every other participant.
for (Participant p : mParticipants) {
if (p.getParticipantId().equals(mMyId)) {
continue;
}
if (p.getStatus() != Participant.STATUS_JOINED) {
continue;
}
// final score notification must be sent via reliable message
mRealTimeMultiplayerClient.sendReliableMessage(mLetterBuf,
mRoomId, p.getParticipantId(), new RealTimeMultiplayerClient.ReliableMessageSentCallback() {
@Override
public void onRealTimeMessageSent(int statusCode, int tokenId, String recipientParticipantId) {
//Log.d(TAG, "RealTime message sent--------->"+mLetterBuf[0]);
}
})
.addOnSuccessListener(new OnSuccessListener<Integer>() {
@Override
public void onSuccess(Integer tokenId) {
Log.d(TAG, "Created a reliable message with tokenId: " + tokenId);
}
});
}
}
随机抽取字母的方法:
public byte[] randomLetter(){
String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random rnd = new Random();
char c = chars.charAt(rnd.nextInt(chars.length()));
String letra = String.valueOf(c);
byte[] by_new = letra.getBytes();
return by_new;
}
这里是我要阅读接收消息的地方(在这里,我收到的字母不同于方法randomLetter()
中生成的字母-这是在我调用与同伴共享字母的方法之前的< / p>
OnRealTimeMessageReceivedListener mOnRealTimeMessageReceivedListener = new OnRealTimeMessageReceivedListener() {
@Override
public void onRealTimeMessageReceived(@NonNull RealTimeMessage realTimeMessage) {
byte[] buf = realTimeMessage.getMessageData();
String sender = realTimeMessage.getSenderParticipantId();
Log.d(TAG, "Letter received: " + (char) buf[0]); <-------Problem is here
updatePeersLetter(str);
(...)
有人能理解为什么我在发送消息之前和接收消息之后收到不同的信件吗?