2014-12-20 3 views
0

applicationDidBecomeActive가 실행될 때 장면이 자동으로 applicationWillResignActive 및 autoPopauses되면 일시 중지됩니다. applicationDidBecomeActive가 실행될 때 nsnotification을 통해 applicationWillResignActive에서 일시 중지하고 자동으로 다시 시작하지 않도록해야합니다. 어떤 아이디어? 미리 감사드립니다.SpriteKit 자동 실행 및 자동 일시 중지 iOS8

AppDelegate에

- (void)applicationDidBecomeActive:(UIApplication *)application { 
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
[[NSNotificationCenter defaultCenter] postNotificationName:@"backgroundPause" object:nil]; 
} 

GameViewController

- (void)handleNotification:(NSNotification *)notification { 

if ([notification.name isEqualToString:@"backgroundPause"]) { 
    SKView *skView = (SKView *)self.view; 
    skView.scene.paused = YES; //pauses scene 

    [self.lblPaused removeFromSuperview];//removes any lingering pause menu items 
    [self.lblPausedHelp removeFromSuperview]; 

    self.lblPaused = [[UILabel alloc] init]; 
    self.lblPaused.center = CGPointMake(self.view.frame.size.width/2 - 125, self.view.frame.size.height/2 - 40); 
    self.lblPaused.text = @"PAUSED"; 
    [self.lblPaused setFont:[UIFont boldSystemFontOfSize:66]]; 
    [self.lblPaused sizeToFit]; 
    self.lblPaused.textColor = [UIColor blackColor]; 
    [self.view addSubview:self.lblPaused];//adds pause label 

    self.lblPausedHelp = [[UILabel alloc] init]; 
    self.lblPausedHelp.center = CGPointMake(self.view.frame.size.width/2 - 145, self.view.frame.size.height/2 + 40); 
    self.lblPausedHelp.text = @"tap anywhere to resume"; 
    [self.lblPausedHelp setFont:[UIFont boldSystemFontOfSize:26]]; 
    [self.lblPausedHelp sizeToFit]; 
    self.lblPausedHelp.textColor = [UIColor blackColor]; 
    [self.view addSubview:self.lblPausedHelp];//adds pause label 
} 

} 나는 그것이 spritekit 버그의 확신

+0

의 "법과 일시 정지, 일시 정지보기 ld " – LearnCocos2D

답변

1

. 당신이 무엇을 하든지, 게임은 스스로 잠에서 멈추지 않을 것입니다. applicationDidBecomeActive

나는 여기에 같은 질문을했습니다. pausing spritekit game on app launch/exit .. iOS8 SKScene을 서브 클래스 화하고 paused 속성을 재정 의하여 작동해야합니다. 당신이 그렇게해야한다는 것이 이상합니다. 그것은 정말 많은 문제를 가지고 있지만 shoulds 내 게임을 일시 중지 머물 수있는 유일한 방법입니다

편집 : 좋아, 내가 코드를 objective-c로 번역했습니다. 내 목표 - c는 내가 예상했던 것보다 녹슬었기 때문에 이것이 당신에게 유용하길 바란다.

AppDelegate.m

- (void)applicationWillResignActive:(UIApplication *)application { 
    [[NSNotificationCenter defaultCenter]postNotificationName:@"pauseGameScene" object:nil]; 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    [[NSNotificationCenter defaultCenter]postNotificationName:@"stayPausedNotification" object:nil]; 
} 

SKView 아강

@interface MySKView : SKView 
- (void) setStayPaused; 
@end 

@implementation MySKView 

bool _stayPaused = false; 

- (void) setPaused:(BOOL)paused{ 
    if (!_stayPaused) { 
     super.paused = paused; 
    } 
    _stayPaused = NO; 

} 


- (void) setStayPaused{ 
    _stayPaused = YES; 
} 

@end 

GameViewController

@interface GameViewController : UIViewController 

-(void)pauseGame; 

@end 

@implementation GameViewController 

SKScene *_scene; 
MySKView *_skView; 

-(void)pauseGame{ 
    _skView.paused = YES; 
    _skView.scene.view.paused = YES; 
} 


- (void)viewDidLoad 
{ 

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pauseGame) name:@"pauseGameScene" object:nil]; 


    // Configure the view. 

    _skView = [[MySKView alloc]initWithFrame:self.view.frame]; 

    _skView.showsFPS = YES; 
    _skView.showsNodeCount = YES; 
    /* Sprite Kit applies additional optimizations to improve rendering performance */ 
    _skView.ignoresSiblingOrder = YES; 

    // Create and configure the scene. 
    _scene = [[GameScene alloc]initWithSize:_skView.frame.size]; 
    _scene.scaleMode = SKSceneScaleModeAspectFill; 

    // Present the scene. 
    [self.view addSubview:_skView]; 
    [_skView presentScene:_scene]; 

} 

- (void)viewDidAppear:(BOOL)animated{ 
    [[NSNotificationCenter defaultCenter]addObserver:_skView selector:@selector(setStayPaused) name:@"stayPausedNotification" object:nil]; 
} 


@end 
+0

objective-c에서도 똑같이 작동합니까? – bolencki13

+0

ive가 내 대답을 편집했습니다. 나는 이것을 시험했고 그것은 나를 위해 일했다. – hamobi

+0

정말 고마워요. – bolencki13

관련 문제