Discussion in 'iOS Programming' started by helpfulzerox, Apr 18, 2012.

  1. helpfulzerox macrumors newbie

    Apr 18, 2012
    I am trying to setup my game so that when one user selects a mode/button, the other user cannot select that mode/button, and have to choose the other button. I am using bluetooth connection, and it almost works, but no matter what the user chooses, it just hides my attack button. I have two buttons, attackButton and hungerButton. (I have the GameKit Framework included.) PLEASE HELP ME!

    My .h file:
    #import <UIKit/UIKit.h>
    #import <GameKit/GameKit.h>
    #import "Definitions.h"
    @interface Multiplayer : UIViewController <UIApplicationDelegate, GKPeerPickerControllerDelegate, GKSessionDelegate> {
        GKPeerPickerController  *mPeerPicker;
        GKSession               *mSession;
        IBOutlet UILabel *chosenMode;
        IBOutlet UIButton *attackButton;
        IBOutlet UIButton *hungerButton;
        NSTimer *checkToSeeWhatMode;
        int whatbuttontohidecounter;
    @property (nonatomic, retain) GKPeerPickerController    *mPeerPicker;
    @property (nonatomic, retain ) GKSession                *mSession;
    -(void) nullifySession;
    My .m file
    #import "Multiplayer.h"
    @implementation Multiplayer
    @synthesize mPeerPicker, mSession;
    -(IBAction)attack {
        [self.mSession sendDataToAllPeers:[@"YES" dataUsingEncoding:NSStringEncodingConversionAllowLossy] withDataMode:GKSendDataReliable error:nil];
    -(IBAction)hunger {
        [self.mSession sendDataToAllPeers:[@"NO" dataUsingEncoding:NSStringEncodingConversionAllowLossy] withDataMode:GKSendDataReliable error:nil];
    -(void)viewDidLoad {
            self.mPeerPicker = [[GKPeerPickerController alloc] init];
            mPeerPicker.delegate = self;
            mPeerPicker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
            [mPeerPicker show];
    - (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type{  
        self.mSession = [[GKSession alloc] initWithSessionID:@"MySession" displayName:nil sessionMode:GKSessionModePeer];
        mSession.delegate = self;
        return mSession;
    - (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker{
        [self nullifySession];
    - (void)session:(GKSession *)session didFailWithError:(NSError *)error{
        [self nullifySession];
    - (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session{
        [mSession setDataReceiveHandler:self withContext:NULL];
        [mPeerPicker dismiss];
        self.mPeerPicker = nil;
    - (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context{
       chosenMode.text = [NSString stringWithFormat:@"%@" ,/*[mSession displayNameForPeer:peer],*/ [NSString stringWithUTF8String:[data bytes]]];
        checkToSeeWhatMode = [NSTimer scheduledTimerWithTimeInterval:0.01
                                                              target:self selector:@selector(whatModeIsTaken)userInfo:nil repeats:YES];
    -(void)whatModeIsTaken {
       attackButton.hidden = [NSString stringWithFormat:@"%@",chosenMode.text];
        if (attackButton.hidden == YES) {
            hungerButton.hidden = NO;
    -(void) nullifySession{
        if(mSession) {
            [mSession disconnectFromAllPeers]; 
            [mSession setDataReceiveHandler: nil withContext: NULL]; 
            self.mSession = nil;
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
  2. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    What datatype is a UIButton's hidden property?

Share This Page