https://www.makeschool.com/docs/?source=mgwu#!/cocos2d/1.0/scenes

Scenes in Cocos2D

In Cocos2D you have to have a CCScene as the root node of your scene graph. CCScene is a subclass of CCNode. Any CCNode subclass can be added to a scene by using the addChild: method. By default any CCScene is initialized as a full screen node. Only one CCScene can be active at a time. The currenty active scene is considered on the stage.

Scene lifecycle

Cocos2D provides multiple lifecyle events that can be relevant for your scenes and other nodes in your scene. These events are trigerred when a scene gets initialized and when it becomes active/inactive. Each of the scene transition events is represented by one method in CCNode that gets called by the CCDirector. You can override these methods in any CCNode. The Objective-C default initializer method init gets called when scene/node is initialized from Cocos2D code. When you created your scene/node in SpriteBuilder didLoadFromCCB is the designated initializer method. Here is an overview of all the lifecycle methods:

  • init: The default Obj-C initializer. If the scene is not created in SpriteBuilder you mostly use this method to build the scene by adding different nodes as children.

  • didLoadFromCCBIf you created your scene in SpriteBuilder this method is called when the complete scene is loaded and all code connections are set up. You implement this method to access and manipulate the content of the scene. You cannot access child nodes of the scene or code connection variables before this method is called.

  • onEnter: This method gets called as soon as the replacement of the currently active scene with this scene begins. If you are using an animated scene transition this method will be called at the beginning of the transition. You can add any code that shall be executed when the scene starts to enter the stage.

  • onEnterTransitionDidFinish: If you are using an animated scene transition this method will be called when the transition finishes. If you are not using transitions this method is called at the same time as onEnter. Implement this method to execute code once the scene has fully entered the stage.

  • onExit: Is called when the scene leaves the stage. If the scene leaves the stage with a transition, this event is called when the transition finishes.

  • onExitTransitionDidStart: Is called when the scene leaves the stage. If the scene leaves the stage with a transition, this event is called when the transition starts.

Note: you have to call the super implementation when you override onEnteronEnterTransitionDidFinishonExit or onExitTransitionDidStart.

Scene Graph and Scene Management

Each CCScene stores a hierarchy of different CCNode instances. CCScene itself is an invisible node that is only used as a container. Most CCNodesubclasses are used to present an object on the screen (a single color surface or a texture). The hierarchical structure of CCNode instances is called the scene graph.

The following diagram shows multiple different scenes and scene graphs of a game: 

The diagram represents three different scenes of which each contains multiple nodes. There are a couple of takeaways from this diagram:

  • CCDirector is the instance that controls which scene gets presented on the stage. One can tell CCDirector to present the main menu, the gameplay scene or the highscore scene. CCDirector will only present one scene at a time.
  • CCScenes are used to build a logical group of objects. In Cocos2D this always means one scene represents one screen. One will have scenes for menus, leaderboards, gameplay, etc. Scenes itself don't have a representation, their sole goal is to group CCNode instances.
  • CCNodes Basically anything that is visible in Cocos2D is a subclass of CCNode.

Games in Cocos2D consist of different scenes. The flow of the game is defined by telling the CCDirector when which scene shall be presented (menu first, then gameplay, then leaderboard). The content of the scenes is a compilation of different nodes. A CCNode can again contain other CCNode instances. The hierarchy of CCNode instances in a scene is called the scene graph.

Creating a Scene

The default SpriteBuilder and Cocos2D projects come with one scene. For most games you will have to create additional scenes. You can either create scenes in SpriteBuilder or in code.

  • Create a Scene CCB-File in SpriteBuilder and load the scene in code utilizing CCBReader:

      CCScene *gameplayScene = [CCBReader loadAsScene:@"Gameplay"];
  • Create a subclass of CCScene in Xcode and intialize:

      CCScene *gameplayScene = * [GameplayScene scene];

When to create the Scene content

For most games you will create the content for your scenes in SpriteBuilder or in the init method of your CCScene subclass. However, when certain scenes need to allocate a lot of memory it can be helpful to create the actual scene content in the onEnter method and removing it when onExit is called. This way the memory is only used when the scene is active on the stage.

Switching between Scenes

The CCDirector is responsible for presenting and hiding scenes. The easiest way to replace a scene, without a transition is using the replaceScene: method:

[[CCDirector sharedDirector] replaceScene:gameplayScene];

Using this method the CCDirector will replace the currently active scene with the gameplayScene.

Transitioned Scene Changes

If you want to provide a transition effect for swapping the scene you can use the replaceScene:withTransition:. It takes a CCTransition as parameter. A CCTransition can be easily created using one of the initializer methods that provide pre defined transition effects:

CCTransition *transition = [CCTransition transitionCrossFadeWithDuration:1.f];
[[CCDirector sharedDirector] replaceScene:gameplayScene withTransition:transition];

To get an overview of the different available transitions and the transition options, take a look at the Cocos2D class reference for CCTransition.

Using the Scene Navigation stack

Another option instead of replacing scenes is pushing and popping scenes. When you push a new scene, the old scene gets deactivated but remains in a the stack of scenes. When you pop a scene that scene gets removed from the navigation stack and the top most controller of the navigation stack gets presented.

Cocos2D provides methods to pop and push scenes with or without transitions:

  • pushScene:withTransition:
  • popScene:withTransition:
  • pushScene:
  • popScene

Additionally there is a popToRootScene method that empties the complete navigation stack and returns to the first scene.

When building User Interfaces with multiple scenes (e.g. menu structures) you should use the navigation stack instead of manually replacing scenes. The most common pattern is to provide a back button that pops the latest scene on the navigation stack.

Scenes in Cocos2D的更多相关文章

  1. 高屋建瓴 cocos2d-x-3.0架构设计 Cocos2d (v.3.0) rendering pipeline roadmap(原文)

    Cocos2d (v.3.0) rendering pipeline roadmap Why (the vision) The way currently Cocos2d does rendering ...

  2. cocos2d-x项目101次相遇: Scenes , Director, Layers, Sprites

    cocos2d-x 101次相遇 / 文件夹  1   安装和环境搭建 -xcode  2   Scenes , Director, Layers, Sprites 3   建立图片菜单  4   在 ...

  3. cocos2d Programming Guide

    http://python.cocos2d.org/doc/programming_guide/index.html The cocos2d Programming Guide provides in ...

  4. cocos2d学习网址

    http://python.cocos2d.org/doc/programming_guide/index.html http://bbs.tairan.com/article-25-1.html h ...

  5. 小尝试一下 cocos2d

    好奇 cocos2d 到底是怎样一个框架,正好有个项目需要一个游戏框架,所以稍微了解了一下.小结一下了解到的情况. 基本概念 首先呢,因为 cocos2d 是基于 pyglet 做的,你完全可以直接用 ...

  6. 采用cocos2d-x lua 制作数字滚动效果样例

    require "Cocos2d"require "Cocos2dConstants"local testscene = class("testsce ...

  7. Cocos2d 利用继承Draw方法制作可显示三维数据(宠物三维等)的三角形显示面板

    很久没有写博客了,这段时间比较忙,又是搬家又是做自己的项目,还有太多琐碎的事情缠身,好不容易抽出时间把最近自己做的一些简单例子记录一下. 在我的项目中,我需要一个显示面板来显示游戏中的一个三维数据,例 ...

  8. iPhone开发与cocos2d 经验谈

    转CSDN jilongliang : 首先,对于一个完全没有mac开发经验,甚至从没摸过苹果系统的开发人员来说,首先就是要熟悉apple的那一套开发框架(含开发环境IDE.开发框架uikit,还有开 ...

  9. cocos2d学习记录

    视频 - http://www.manew.com/forum-105-3.html一个论坛帖 - http://www.zhihu.com/question/21114802官网 - http:// ...

随机推荐

  1. E - Nature Reserve CodeForces - 1059D

    传送门 There is a forest that we model as a plane and live nn rare animals. Animal number iihas its lai ...

  2. Android面试收集录4 Fragment详解

    1.什么是Fragment? 你可以简单的理解为,Fragment是显示在Activity中的Activity. 它可以显示在Activity中,然后它也可以显示出一些内容. 因为它拥有自己的生命周期 ...

  3. Windows GitLab使用全过程

    1.首先安装Git 1.1.下载网站: https://git-for-windows.github.io/ 1.2.安装Git参考网站 http://blog.csdn.net/u012614287 ...

  4. PHP代码审计1-审计环境与调试函数

    审计环境与调试函数 审计环境 测试环境 常用集成环境:phpStudy.WampServer #不同的操作系统下,漏洞测试的结果也可能不一样 PHP编写工具 EditPlu Notepad++ 代码审 ...

  5. 挂个AC自动机

    struct ACM{ ],f[N],cnt[N]; int sz,rt; int ins(char *s){ int n=strlen(s),u=rt; ;i<n;i++){ int c=s[ ...

  6. Kafka实践、升级和新版本(0.10)特性预研

    本文来自于网易云社区 一.消息总线MQ和Kafka (挡在请求的第一线) 1. 几个应用场景 case a:上游系统往下游系统推送消息,而不关心处理结果: case b:一份新数据生成,需要实时保存到 ...

  7. 剑指Offer - 九度1386 - 旋转数组的最小数字

    剑指Offer - 九度1386 - 旋转数组的最小数字2013-11-24 01:57 题目描述: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转 ...

  8. 《Cracking the Coding Interview》——第8章:面向对象设计——题目8

    2014-04-23 23:49 题目:有个棋牌游戏叫Othello,也叫Reversi.请看游戏规则.中文应该叫黑白棋吧,不常玩儿就是了. 解法:既然这题的规则很清楚,也很清楚,我就写了一个命令行的 ...

  9. heat launch an instance

    在包含Orchestration服务的环境中,可以创建启动实例的堆栈 创建yam文件 heat_template_version: 2015-10-15 description: Launch a b ...

  10. Python——数据类型之list、tuple

    本篇主要内容 •  list初识 •  list元素的访问 •  list内部所有的方法 •  tuple介绍和与list用法的比较 我觉得Python里面用的最多的就是List了,感觉好强大.他能存 ...