1

我想通过游戏中心在游戏的其他玩家之间交换玩家的当前位置信息。我经历过这个

但是哪个课程帮助我使位置转移成为可能?请帮我。

4

1 回答 1

2
-(void)sendData
{  
NSString *mystring=[NSString stringWithFormat:@"lat=22.810000log=71.477989user=%@",lp.alias];
    int myscore=20;
    NSError *error;
    NSData* data=[mystring dataUsingEncoding: [NSString defaultCStringEncoding] ];
    BOOL success = [match sendDataToAllPlayers:data withDataMode:GKPlayerStateConnected error:&error];
    if (!success) {
        NSLog(@"Error sending init packet:\n%@",error);
    }
}

为了发送数据,其中 match 是 GKMatch 的对象,为了接收数据,使用以下委托方法

- (void)match:(GKMatch *)theMatch didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID {    


    if (match != theMatch) return;
    NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"Received String : %@",string);

    NSString* str_lat =[[[[string componentsSeparatedByString:@"lat="]objectAtIndex:1] componentsSeparatedByString:@"log="]objectAtIndex:0];


     NSString* str_long =[[[[string componentsSeparatedByString:@"log="]objectAtIndex:1] componentsSeparatedByString:@"user="]objectAtIndex:0];

    NSRange range = [string rangeOfString:@"user="];
    NSString *str_username = [[string substringFromIndex:NSMaxRange(range)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];


    NSLog(@"str_lat%@",str_lat);
    NSLog(@"str_long%@",str_long);

    NSLog(@"str_username--%@",str_username);
}
于 2012-10-01T12:08:18.030 回答