XcodeTips

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
      
      // 通知を受け取る処理
      NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
      // 通知センターにオブザーバ(通知を受け取るオブジェクト)を追加
      [nc addObserver:self
             selector:@selector(receiveGCConnect:)
                 name:@"GCControllerDidConnectNotification"
               object:nil];
      [nc addObserver:self
             selector:@selector(receiveGCDisConnect:)
                 name:@"GCControllerDidDisconnectNotification"
               object:nil];
      
    }
    return self;
}
// ゲームコントローラーが接続されたメッセージ
-(void)receiveGCConnect:(NSNotification*)center {
  self.myLabel3.text = @"Connected";
  [GCController startWirelessControllerDiscoveryWithCompletionHandler:^{
    NSArray * controllers = [GCController controllers];
    for (GCController * c in controllers){
      __weak typeof(self) weakSelf = self;
      c.controllerPausedHandler = ^(GCController *controller) {
        weakSelf.paused = !weakSelf.paused;
        weakSelf.myLabel.text = weakSelf.paused ? @"Pause" : @"Ready";
      };
      
      if(self.gamepad == nil && c.gamepad != nil) {
        self.gamepad = c.gamepad;
        self.myLabel.text = c.vendorName;
        self.gamepad.valueChangedHandler = ^(GCGamepad *gpad, GCControllerElement *element){
          NSString* str = @"";
          if (gpad.buttonA.isPressed) str = [str stringByAppendingString: @"A"];
          if (gpad.buttonB.isPressed) str = [str stringByAppendingString: @"B"];
          if (gpad.buttonX.isPressed) str = [str stringByAppendingString: @"X"];
          if (gpad.buttonY.isPressed) str = [str stringByAppendingString: @"Y"];
          if (gpad.rightShoulder.isPressed) str = [str stringByAppendingString: @"sR"];
          if (gpad.leftShoulder.isPressed) str = [str stringByAppendingString: @"sL"];
          if (gpad.dpad.up.isPressed) str = [str stringByAppendingString: @"U"];
          if (gpad.dpad.down.isPressed) str = [str stringByAppendingString: @"D"];
          if (gpad.dpad.left.isPressed) str = [str stringByAppendingString: @"L"];
          if (gpad.dpad.right.isPressed) str = [str stringByAppendingString: @"R"];
          weakSelf.myLabel.text = [NSString stringWithFormat:@"%@(%.3f,%.3f)"
                                   , str
                                   , gpad.dpad.xAxis.value
                                   , gpad.dpad.yAxis.value];
        };
      }
      if (self.exgamepad == nil && c.extendedGamepad != nil) {
        self.myLabel2.text = c.vendorName;
        self.exgamepad = c.extendedGamepad;
        self.exgamepad.valueChangedHandler = ^(GCExtendedGamepad *gpad, GCControllerElement *element){
          NSString* str = @"";
          if (gpad.buttonA.isPressed) str = [str stringByAppendingString: @"A"];
          if (gpad.buttonB.isPressed) str = [str stringByAppendingString: @"B"];
          if (gpad.buttonX.isPressed) str = [str stringByAppendingString: @"X"];
          if (gpad.buttonY.isPressed) str = [str stringByAppendingString: @"Y"];
          if (gpad.rightShoulder.isPressed) str = [str stringByAppendingString: @"sR"];
          if (gpad.leftShoulder.isPressed) str = [str stringByAppendingString: @"sL"];
          if (gpad.dpad.up.isPressed) str = [str stringByAppendingString: @"U"];
          if (gpad.dpad.down.isPressed) str = [str stringByAppendingString: @"D"];
          if (gpad.dpad.left.isPressed) str = [str stringByAppendingString: @"L"];
          if (gpad.dpad.right.isPressed) str = [str stringByAppendingString: @"R"];
          if (gpad.rightTrigger.isPressed) str = [str stringByAppendingString: @"tR"];
          if (gpad.leftTrigger.isPressed) str = [str stringByAppendingString: @"tL"];
          weakSelf.myLabel2.text = [NSString stringWithFormat:@"%@(%.2f,%.2f)-(%.2f,%.2f)"
                                    , str
                                    , gpad.rightThumbstick.xAxis.value
                                    , gpad.rightThumbstick.yAxis.value
                                    , gpad.leftThumbstick.xAxis.value
                                    , gpad.leftThumbstick.yAxis.value];
        };
      }
    } 
    }];

}

// ゲームコントローラーが切断されたメッセージ
-(void)receiveGCDisConnect:(NSNotification*)center {
  self.myLabel3.text = @"DisConnected";
  [GCController stopWirelessControllerDiscovery];
  self.gamepad = nil;
  self.exgamepad = nil;
}

添付ファイル: fileMyScene.m 326件 [詳細] fileMyScene.h 339件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2014-01-24 (金) 02:12:58 (1638d)