1. <legend id='5HVrb'><style id='5HVrb'><dir id='5HVrb'><q id='5HVrb'></q></dir></style></legend>

      <small id='5HVrb'></small><noframes id='5HVrb'>

      <i id='5HVrb'><tr id='5HVrb'><dt id='5HVrb'><q id='5HVrb'><span id='5HVrb'><b id='5HVrb'><form id='5HVrb'><ins id='5HVrb'></ins><ul id='5HVrb'></ul><sub id='5HVrb'></sub></form><legend id='5HVrb'></legend><bdo id='5HVrb'><pre id='5HVrb'><center id='5HVrb'></center></pre></bdo></b><th id='5HVrb'></th></span></q></dt></tr></i><div id='5HVrb'><tfoot id='5HVrb'></tfoot><dl id='5HVrb'><fieldset id='5HVrb'></fieldset></dl></div>

        <tfoot id='5HVrb'></tfoot>
        • <bdo id='5HVrb'></bdo><ul id='5HVrb'></ul>

        正确重启/重新初始化半单例

        时间:2024-08-11
          <tbody id='erle7'></tbody>
        <legend id='erle7'><style id='erle7'><dir id='erle7'><q id='erle7'></q></dir></style></legend>
      1. <small id='erle7'></small><noframes id='erle7'>

        <tfoot id='erle7'></tfoot>

        <i id='erle7'><tr id='erle7'><dt id='erle7'><q id='erle7'><span id='erle7'><b id='erle7'><form id='erle7'><ins id='erle7'></ins><ul id='erle7'></ul><sub id='erle7'></sub></form><legend id='erle7'></legend><bdo id='erle7'><pre id='erle7'><center id='erle7'></center></pre></bdo></b><th id='erle7'></th></span></q></dt></tr></i><div id='erle7'><tfoot id='erle7'></tfoot><dl id='erle7'><fieldset id='erle7'></fieldset></dl></div>
          <bdo id='erle7'></bdo><ul id='erle7'></ul>

                  本文介绍了正确重启/重新初始化半单例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 cocos2d 游戏的主游戏层/场景中使用半单例方法,如后面的代码所示.

                  I'm using the semi-singleton approach for the main game layer/scene of my cocos2d game as shown in the later code.

                  目标是通过调用 [[GameLayer sharedGameLayer] restart] 方法使用暂停或游戏结束层中的按钮正确重新启动/重新创建此单例场景.

                  The objective is to properly restart/recreate this singleton scene using a button in a pause or gameover layers by calling: [[GameLayer sharedGameLayer] restart] method.

                  问题是如果我为此使用 CCTransition 效果,并使用 sharedGameLayer = nil; 行覆盖 GameLayer 的 dealloc 方法(到确保静态变量的重置),sharedGameLayer 变量在第一次重启后(也就是第一次 dealloc 后)保持为零,所以调用 restart 方法什么都不做.

                  The problem is if I use a CCTransition effect for that, and override the GameLayer's dealloc method with sharedGameLayer = nil; line (to ensure the resetting of the static variable), the sharedGameLayer variable stays nil after the first restart (aka. after the first dealloc), so calling the restart method does nothing.

                  怀疑的方法根本不覆盖 dealloc 方法,而是在使用 replaceScene: 重新启动场景之前,我设置sharedGameLayer 为零.

                  What works with suspicion is not to override the dealloc method at all, but before restarting the scene using replaceScene:, I set the sharedGameLayer to nil.

                  问题:这是重新启动/重新创建这个半单例类的正确方法吗?

                  Question: Is this the right approach for restarting/recreating this semi-singleton class?

                  提前致谢.

                  代码:

                  GameLayer.m:

                  GameLayer.m:

                  static GameLayer *sharedGameLayer;
                  
                  @implementation GameLayer
                  
                  - (id)init
                  {
                      NSLog(@"%s", __PRETTY_FUNCTION__);
                  
                      // always call "super" init
                      // Apple recommends to re-assign "self" with the "super's" return value
                      if (self = [super initWithColor:ccc4(255, 255, 255, 255) width:[CCDirector sharedDirector].winSize.width
                                               height:[CCDirector sharedDirector].winSize.height])
                      {
                  
                          // Set the sharedGameLayer instance to self.
                          sharedGameLayer = self;
                  
                          // Set the initial game state.
                          self.gameState = kGameStateRunning;
                  
                          // Register with the notification center in order to pause the game when game resigns the active state.
                          NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
                          [center addObserver:self selector:@selector(pauseGame) name:UIApplicationWillResignActiveNotification object:nil];
                  
                          // Enable touches and multi-touch.
                          CCDirector *director = [CCDirector sharedDirector];
                          [director.touchDispatcher addTargetedDelegate:self priority:0 swallowsTouches:YES];
                          director.view.multipleTouchEnabled = YES;
                  
                  
                          // Set the initial score.
                          self.score = 5;
                  
                          // Create the spiders batch node.
                          [self createSpidersBatchNode];
                  
                          // Load the game assets.
                          [self loadAssets];
                  
                  
                          // Play Background music.
                          if (![SimpleAudioEngine sharedEngine].isBackgroundMusicPlaying)
                              [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"backgroundmusic.mp3" loop:YES];
                  
                          // Preload sound effects.
                          [[SimpleAudioEngine sharedEngine] preloadEffect:@"inbucket.mp3"];
                          [[SimpleAudioEngine sharedEngine] preloadEffect:@"outbucket.mp3"];
                          [[SimpleAudioEngine sharedEngine] preloadEffect:@"gameover.mp3"];
                  
                          // Schdule updates.
                          [self scheduleUpdate];
                          [self schedule:@selector(releaseSpiders) interval:0.7];
                      }
                  
                      return self;
                  }
                  
                  
                  
                  - (void)createSpidersBatchNode
                  {
                      NSLog(@"%s", __PRETTY_FUNCTION__);
                  
                      // BatchNode. (For Animation Optimization)
                      self.spidersBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"spiderAtlas.png"];
                  
                      // Spider sprite + BatchNode + animation action.
                      for (int i = 0; i < 50; i++) {
                          Spider *spider = [[Spider alloc] init];
                          spider.spiderSprite.visible = NO;
                      }
                  
                      [self addChild:self.spidersBatchNode];
                  }
                  
                  
                  
                  - (void)restartGame
                  {
                      // Play button pressed sound effect.
                      [[SimpleAudioEngine sharedEngine] playEffect:@"button.mp3"];
                  
                      // Nil'ing the static variable.
                      sharedGameLayer = nil;
                  
                      // Restart game.
                      [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[GameLayer scene]]];
                  }
                  
                  
                  
                  + (CCScene *)scene
                  {
                      // 'scene' is an autorelease object.
                      CCScene *scene = [CCScene node];
                  
                      // Game Layer.
                       // 'gameLayer' is an autorelease object.
                      GameLayer *gameLayer = [GameLayer node];
                  
                       // add gameLayer as a child to scene
                      [scene addChild: gameLayer];
                  
                      // HUD Layer.
                      HUDLayer *hudLayer = [HUDLayer node];
                      [scene addChild:hudLayer];
                      gameLayer.hud = hudLayer;
                  
                      // return the scene
                      return scene;
                  }
                  
                  
                  
                  + (GameLayer *)sharedGameLayer
                  {
                      return sharedGameLayer;
                  }
                  
                  
                  
                  - (void)cleanup
                  {
                      [[NSNotificationCenter defaultCenter] removeObserver:self];
                      [[CCDirector sharedDirector].touchDispatcher removeDelegate:self];
                      [self stopAllActions];
                      [self unscheduleAllSelectors];
                      [self unscheduleUpdate];
                  
                      [self removeAllChildrenWithCleanup:YES];
                      self.hud = nil;
                  
                      [super cleanup];
                  }
                  
                  
                  
                  //- (void)dealloc
                  //{
                  //    sharedGameLayer = nil;
                  /
                  • <bdo id='w7jx7'></bdo><ul id='w7jx7'></ul>
                    <legend id='w7jx7'><style id='w7jx7'><dir id='w7jx7'><q id='w7jx7'></q></dir></style></legend>
                  • <tfoot id='w7jx7'></tfoot>
                      <tbody id='w7jx7'></tbody>

                    <small id='w7jx7'></small><noframes id='w7jx7'>

                      <i id='w7jx7'><tr id='w7jx7'><dt id='w7jx7'><q id='w7jx7'><span id='w7jx7'><b id='w7jx7'><form id='w7jx7'><ins id='w7jx7'></ins><ul id='w7jx7'></ul><sub id='w7jx7'></sub></form><legend id='w7jx7'></legend><bdo id='w7jx7'><pre id='w7jx7'><center id='w7jx7'></center></pre></bdo></b><th id='w7jx7'></th></span></q></dt></tr></i><div id='w7jx7'><tfoot id='w7jx7'></tfoot><dl id='w7jx7'><fieldset id='w7jx7'></fieldset></dl></div>