在我的应用程序中,我想在设备旋转(方向更改)的情况下调用CCScene myscene中的某些方法。我禁用了自动旋转(因为我不希望它发生)。
问题是:我想根据我的设备方向在场景中更改重力。我的代码:
-(void) onEnter
{
[super onEnter];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification_OrientationWillChange:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification_OrientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
-(void) onExit
{
//[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
//[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
-(void)notification_OrientationWillChange:(NSNotification*)n
{
orientation = (UIInterfaceOrientation)[[n.userInfo objectForKey:UIApplicationStatusBarOrientationUserInfoKey] intValue];
}
-(void)notification_OrientationDidChange:(NSNotification*)n
{
if (orientation == UIInterfaceOrientationLandscapeLeft) {
b2Vec2 gravity( 0, -10);
world->SetGravity(gravity);
}
if (orientation == UIInterfaceOrientationLandscapeRight) {
b2Vec2 gravity( 0, 10);
world->SetGravity(gravity);
}
}
但是在这种情况下,只有在启用自动旋转时才能收到通知。(如果它被禁用,设备实际上不会更改状态栏的方向)。你能帮我吗?