如何在Cocos2d中添加不受场景转换影响的全局CCLayer?

发布于 2024-12-10 03:40:53 字数 277 浏览 3 评论 0原文

是否可以在Cocos2d中添加不受场景转换影响的全局图层?

正如我所看到的,它应该高于所有场景层次结构。

Cocos2d 论坛上有一个古老而简短的讨论,但没有答案: http://www.cocos2d-iphone.org/forum/topic/8071

UPD。 “通过场景转换”我的意思是“通过动画场景转换”。

Is it possible to add global layer in Cocos2d, which is not affected by scene transitions?

As I can see, it should be above all scenes hierarchy.

There's an old and short discussion on Cocos2d forum, but there's no answer:
http://www.cocos2d-iphone.org/forum/topic/8071

UPD. 'by scene transitions' I mean 'by animated scene transitions'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉态萌生 2024-12-17 03:40:53

您可以使用 CCDirector 的 notificationNode 属性来放置 CCNode(即 CCLayer、CCLabel 等),即使在过渡期间,该 CCNode 也将保留在场景上方。像这样的事情:

CCLayer *layer = [CCLayer node];
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Test" fontName:@"Marker Felt" fontSize:32];
[layer addChild:label];
[[CCDirector sharedDirector] setNotificationNode:layer]; // Layer should be placed here
[layer onEnter]; // Schedule for updates (ie. so that CCActions will work)

它是为了通知目的(广告等),所以我不建议尝试从这个节点做任何太花哨的事情。

You can use the notificationNode property of CCDirector to place a CCNode (ie.CCLayer, CCLabel, etc.) that will remain above scenes, even during transitions. Something like this:

CCLayer *layer = [CCLayer node];
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Test" fontName:@"Marker Felt" fontSize:32];
[layer addChild:label];
[[CCDirector sharedDirector] setNotificationNode:layer]; // Layer should be placed here
[layer onEnter]; // Schedule for updates (ie. so that CCActions will work)

It's meant for notification purposes (ads, etc.) so I wouldn't suggest trying to do anything too fancy from this node.

〃温暖了心ぐ 2024-12-17 03:40:53

我的直觉说,我的大脑说也许

文档说“使用 CCScene 作为所有节点的父节点是一个很好的做法。”

我现在无法测试这一点,但是看看CCNode的继承图,看起来CCNodeCCScene的逻辑只是不同通过锚点。因此,您也许可以创建一个 CCLayer 用作根层,并向其中添加两个子层 - 根 CCSceneCCLayer code> 用于您的 GUI(具有更高的 Z 顺序)。

但是,场景转换可能仍然很棘手,因为您通常调用 CCDirector ReplaceScene,它适用于您提供的根场景。如果您为其提供根CCLayerCCScene 子级,它可能不会绘制CCLayer 及其GUI 子级。如果您给它根CCLayer,您将处于与以前相同的情况。

我还是会尝试一下。

My gut says no, my brain says maybe.

The documentation says "It is a good practice to use and CCScene as the parent of all your nodes."

I can't test this right now, but looking at the inheritence diagram of CCNode, it looks like the logic of CCNode and CCScene differs only by anchor point. So, you might be able to create a CCLayer to use as your root layer, and add two children to it - A root CCScene, and a CCLayer for your GUI (with a higher Z order).

However, scene transitions may still be tricky, as you generally call CCDirector replaceScene, which works on the root scene you give it. If you give it the CCScene child of your root CCLayer, it may not draw the CCLayer and its GUI child. If you give it the root CCLayer, you are in the same situation as before.

I would still give it a try.

可是我不能没有你 2024-12-17 03:40:53

您可以创建一个CCLayer子类并将其变成单例。您可以像任何其他子节点一样将其添加到场景中。

每当从一个场景过渡到另一个场景时,您都可以从旧场景中删除该图层,并将其作为子场景添加到新场景中。仅当您不使用场景过渡动画时,这才有效。

另一种方法是不使用 CCDirector ReplaceScene 方法,而是将应用程序设计为作为永不更改的单个场景运行。要“伪造”场景的更改,您将使用两个图层,一个全局图层和另一个包含当前场景节点的图层。如果您想要过渡,可以使用 CCActions 为图层设置动画,例如滑出屏幕,同时滑入具有不同节点层次结构的新图层。您真正失去的只是 CCSceneTransition 类用于动画场景更改的便利性。

You can create a CCLayer subclass and turn it into a singleton. You add it to the scene like any other child node.

Whenever you transition from one scene to another, you can then remove the layer from the old scene and add it to the new scene as a child. This will only work if you don't use scene transition animations.

The alternative is to not use the CCDirector replaceScene method, and instead design your app to run as a single scene that never changes. To "fake" the changing of a scene you will use two layers, one global layer and another layer that contains your current scene nodes. If you want to transition you can animate the layer with CCActions to, for example, slide out of the screen while sliding in a new layer with a different node hierarchy. All you really lose is the convenience of the CCSceneTransition classes for animating scene changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文