12

デバイスを横向きまたは横向きにすることができるゲームを作成しています。プレーヤーは、一時停止しているときに向きを変更できます。その場合、方向に基づいてゲームが加速度計を解釈する方法を変更する必要があります。

iOS 5では、willRotateToInterfaceOrientationを使用して変更をキャッチし、変数を変更しましたが、iOS6では非推奨になりました。私の既存のコードは次のようになります。

    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)      
        rect = screenRect;

    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
        rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );
    GameEngine *engine = [GameEngine sharedEngine];
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
        engine.orientation = -1;
    } else {
        engine.orientation = 1;
    }
}

置換はUIViewControllerクラスのviewWillLayoutSubviewsメソッドであることを理解しています。私はこのゲームをcocos2d2.1で構築していますが、デモプロジェクトにUIViewControllerクラスがないようです。そのため、このゲームを組み込む方法と、これを機能させるためのコードの外観がわかりません。

4

1 に答える 1

36

デバイスの向きの変更をリッスンします。

[[NSNotificationCenter defaultCenter] 
       addObserver:self
          selector:@selector(deviceOrientationDidChangeNotification:) 
              name:UIDeviceOrientationDidChangeNotification 
            object:nil];

通知されたら、UIDeviceからデバイスの向きを取得します。

- (void)deviceOrientationDidChangeNotification:(NSNotification*)note
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    switch (orientation)
    {
        // etc... 
    }
}
于 2012-10-01T21:37:33.967 に答える