0

我想创建一个继承自CCLayer. 我的原因是因为我有一个单图像全屏CCSprite,我想覆盖在我的应用程序的每个场景上。创建一个基类,并添加一个CCSprite包含图像作为最顶层的 Z 对象似乎是有意义的,因为它可以防止我不得不为每个场景一次又一次地重新编码相同的覆盖实现。

我已经能够CCLayer相对容易地派生一个类。但是,我无法弄清楚如何正确创建另一个图层类的场景CCLayer,该图层类是. 如何做到这一点,如何工作?

据我了解,当大多数用户提出此类问题时,第一个问题是“向我们展示您的代码”。我可以向您展示代码,但我最感兴趣的是 Cocos2d 对象的一个​​非常通用的实现,它派生自CClayer其他层并可用作其他层的基类,预先连接常见的精灵和对象。

4

1 回答 1

0

I think this may be your problem:

"I cannot figure out how to create a scene that is a child, of a child of CCLayer."

As I understand it, Cocos layers are added as children to Cocos scenes, not the other way around, as the quote seems to imply.

I think you could simply make a custom layer class, deriving it from CCLayer and adding your CCSprite. Then simply go to everywhere you are creating or deriving a CCLayer and instead create or derive it from your custom layer class and then show or hide the sprite when needed.

Alternatively, and probably more easily, you could create a category on CCLayer adding a "showFullscreenSprite" method which simply creates the sprite and sets its image, then calls

[self addChild:yourSprite z:yourSprite.zOrder tag:9999];

You would also need a corresponding "hideFullscreenSprite" method which would simply do this

[self removeChildByTag:9999 cleanup:YES];

The nice thing about this approach is you wouldn't need to sub-class at all and all of your CCLayers would now have your "showFullscreenSprite" and "hideFullscreenSprite" methods available.

(Note: "9999" has to be some number you're not already using as a CCNode tag. Make it big enough so you don't have to worry about it. Maybe pull it out into a constant such as "FULL_SCREEN_SPRITE_TAG" or some such for readability.)

Hope this helps!

于 2013-01-23T22:26:22.273 回答