Bluetooth

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

  1. helpfulzerox macrumors newbie

    Joined:
    Apr 18, 2012
    #1
    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:
    Code:
    #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;
    
    
    
    }
    
    
    
    -(IBAction)attack;
    
    -(IBAction)hunger;
    
    
    
    
    
    @property (nonatomic, retain) GKPeerPickerController    *mPeerPicker;
    
    @property (nonatomic, retain ) GKSession                *mSession;
    
    -(void) nullifySession;
    
    
    
    
    
    @end
    
    My .m file
    Code:
    #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 {
    
    
    
    
    
    
    
        if(mSession){
    
        }else{
    
            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);
    
    }
    
    
    
    @end
    
     
  2. dejo Moderator

    dejo

    Staff Member

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

Share This Page