1

我有 2 个 CCLayer 需要在单独的 .m 文件中相互通信

  • Level1.m (CCScene with Level1 CCLayer) - 保存平铺地图和玩家精灵
  • HUDLayer.m(链接到 Level1.m 的顶部) - 保存所有按钮

如何在 HUDLayer.m 中获取以下代码以与 Level1.m 中的玩家精灵对话?

- (void)MoveUpSelected {
        CCMoveTo* moveup = [CCMoveBy actionWithDuration:1  position:ccp(0,-100)];
        CCSequence* sequence = [CCSequence actions: moveup, nil];
        [Player runAction:sequence];
          }

请帮助我已经坚持了好几天了。至少如果有人能指出我正确的方向。谢谢!

4

2 回答 2

1

I would advice you to use you scene object to control communication between its layers. You can create a HUD protocol and set the scene as its delegate. And for each HUD event the scene will react accordingly by accessing the proper layer (stored as its member).

This way you won't have to make this layer coupling.

于 2013-03-24T08:43:26.187 回答
0

要访问另一层,您需要对其进行引用。有很多方法可以做到这一点。在您的情况下,只需为 CCScene 类的每一层添加一个属性。然后图层可以通过其父级访问场景:

CCLayer* otherLayer = [(YourActualSceneClass*)self.parent otherLayer];

非常重要的是不要在任一层中存储对另一层的引用,或者如果这样做,请确保将其设为弱引用,或者在清理方法中将其设为 nil。否则,您创建了一个保留周期。

您将在此处找到有关访问其他节点的更多信息

于 2013-03-23T23:53:14.043 回答