iOS GameKit:通过线路发送浮动
我正在构建一个原型游戏并连接了一些多人游戏功能。不过,我在通过电线发送浮点数时遇到了问题。好奇是否有人遇到过这个问题,或者可以告诉我问题是什么。
我发送的结构如下所示:
struct TRMessageHeader {
int length;
int code;
};
struct TRMessagePushPeer {
struct TRMessageHeader header;
float force;
};
我通过线路发送它的代码如下所示:
- (void) testFloat:(int) floatValue {
struct TRMessagePushPeer message = {0};
message.header.code = TRMessageCodePushPeer;
message.header.length = sizeof(message);
message.force = floatValue;
NSArray * peers = [_session peersWithConnectionState:GKPeerStateConnected];
NSData * data = [NSData dataWithBytes:&message length:sizeof(message)];
NSError * error = NULL;
[_session sendData:data toPeers:peers withDataMode:GKSendDataReliable error:&error];
}
我的接收代码如下所示:
- (void) onPeerPushed:(NSNotification *) notification {
NSDictionary * userInfo = [notification userInfo];
NSData * messageData = [userInfo objectForKey:@"messageData"];
struct TRMessagePushPeer * message = (struct TRMessagePushPeer *)[messageData bytes];
NSLog(@"peer sent float :%f",message->force);
}
所以我遇到的问题是,如果我调用 testFloat:0.4573,则接收对等方的浮点值是0.如果我调用 testFloat:1.3458 则接收对等方的浮点值为 1。
所以看起来它在对等方接收代码中向下舍入,但我不确定为什么。
我认为使用 IEEE754 对浮点进行编码是可行的,但我想知道为什么在编码之前它不能按原样工作。
I'm building a prototype game and have some multiplayer functionality hooked up. I am running into an issue with sending a float over the wire though. Curious if anyone has run into this, or can tell me what the issue is.
The structure that I send looks like this:
struct TRMessageHeader {
int length;
int code;
};
struct TRMessagePushPeer {
struct TRMessageHeader header;
float force;
};
My code that sends it over the wire looks like this:
- (void) testFloat:(int) floatValue {
struct TRMessagePushPeer message = {0};
message.header.code = TRMessageCodePushPeer;
message.header.length = sizeof(message);
message.force = floatValue;
NSArray * peers = [_session peersWithConnectionState:GKPeerStateConnected];
NSData * data = [NSData dataWithBytes:&message length:sizeof(message)];
NSError * error = NULL;
[_session sendData:data toPeers:peers withDataMode:GKSendDataReliable error:&error];
}
My receiving code looks like this:
- (void) onPeerPushed:(NSNotification *) notification {
NSDictionary * userInfo = [notification userInfo];
NSData * messageData = [userInfo objectForKey:@"messageData"];
struct TRMessagePushPeer * message = (struct TRMessagePushPeer *)[messageData bytes];
NSLog(@"peer sent float :%f",message->force);
}
So the issue I'm having is if i call testFloat:0.4573, then the receiving peer's float value is 0. If I call testFloat:1.3458 then the receiving peer's float value is 1.
So it seems like it's rounding down in the peer receive code but I'm not sure why.
I figure encoding the float with IEEE754 would work, but I would like to know why this doesn't work as is before I encode it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
- (void) testFloat:(int) floatValue
...我认为这将是你的问题 - 你在方法声明中将 float 转换为 int 。当它传入时,它会自动转换为 int。一旦你看到这个,你可能会踢自己!
将其更改为:
- (void) testFloat:(float) floatValue
- (void) testFloat:(int) floatValue
...I think that would be your problem - you're casting your float as an int in the method declaration. It will be automatically cast to an int when it is passed in. You will probably kick yourself once you see this!
Change it to this:
- (void) testFloat:(float) floatValue
不要使用 C 风格的结构来发送通知。 NSNotification 的用户信息字典旨在与 Objective C 对象一起使用。创建一个 TRMessagePushPeer 对象并将浮点数作为成员放入(浮点数或 NSNumber 对象)。
它可以很简单:
Don't use a C-style struct for notifications. NSNotification's user info dictionary are meant to work with Objective C objects. Create a
TRMessagePushPeer
object and put the float in as a member (either a float or aNSNumber
object).It can be as simple as: