【问题标题】:how to close layer that showed in other layer in cocos2d-iphone如何关闭 cocos2d-iphone 中其他层中显示的层
【发布时间】:2013-10-20 15:02:32
【问题描述】:

我有一层叫alayer,还有一个叫abutton的按钮,当点击按钮时,另一个叫blayer的层会显示在一个layer中,而不是replaceScene,请看下面的代码,

图层.m

-(void)abuttonclicked:(id)sender
{
  blayer *blayer = [blayer node];
  blayer.position = ccp(1,1);
  [self addChild:blayer];
}

blayer.m有一个叫bbutton的按钮和一个叫bstring的字符串值,我想点击b按钮,它会关闭blayer(从alayer中删除blayer),并将字符串值bstring传递给alayer,请看下面的代码,

 -(void)bbuttonclicked:(id)sender
 {
  // how can do here to close its self(remove its self from alayer), and pass the bstring to alayer?
 }

谢谢。

ps。我可以使用 NSUserDefault 来传递字符串值,但我认为这是一种不好的方式,还有其他方式来传递值吗?

【问题讨论】:

    标签: cocos2d-iphone cclayer ccscene


    【解决方案1】:

    也许传递你可以在 ALayer.h/.m 中声明和实现属性的字符串

     @property(nonatomic,copy) NSString *stringFromLayerB;
    

    在 bButtonClicked 时移除 bLayer:

     -(void)bbuttonclicked:(id)sender
     {
         ALayer *lay = (ALayer*) self.parent;
         lay.stringFromLayerB = @"Whatever you want to set";
         [self removeFromParentAndCleanup:YES];
     }
    

    还有其他方法可以做到这一点。您可以实现回调机制、使用通知、某种委托协议绑定 BLayer 和 ALayer。一切都取决于您的真实(未说明)要求是什么。

    【讨论】:

      【解决方案2】:

      考虑到您的情况,我认为最好使用 NSNotificationCenter。 您可以在点击按钮时从 blayer 发布通知,并在 alayer 中添加观察者以准确响应想要的方式。

      在 alayer 的 init 中添加 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"BlayerNotification" object:nil];,在其 dealloc 中添加 [[NSNotificationCenter defaultCenter] removeObserver:self];

      它的选择器如下:

      - (void)receivedNotification:(NSNotification *)notification {
          NSString *string = (NSString *)notification.object;
          NSLog (@"String received %@", string);
      }
      

      现在您可以在点击 hte 按钮时从 blayer 发布通知:

      -(void)bbuttonclicked:(id)sender {
          [[NSNotificationCenter defaultCenter] postNotificationName:@"BlayerNotification" object:self];
          [self removeFromParentAndCleanup:YES];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多