Scenes

A scene (implemented with the CCScene object) is more or less an independent piece of the app workflow. Some people may call them “screens” or “stages”. Your app can have many scenes, but only one of them is active at a given time.

For example, you could have a game with the following scenes: Intro, Menu, Level 1, Cutscene 1, Level 2, Winning cutscene, losing cutscene, High scores screen. You can think of each one of these scenes as a separate application that can be connected to other scenes with a small amount of "glue" code. For example, the intro scene might go to the menu scene when it finishes, and the scene for Level 1 might lead to cutscene 1 (if the player wins) or to the losing cutscene (if the player loses). An example of how scenes might flow in a game follows:

A cocos2d CCScene is composed of one or more CCNodes, added as children to the scene. Subclasses of CCNode, such as CCLayer and CCSprite, give the scene its appearance and behavior. Typically, you implement your screens as subclasses of CCLayer and add them to a blank instance of CCScene. Then, implement your other graphics and game objects as CCNodes and add them as children to the CCLayer you created.

Because scenes are a subclass of CCNode, they can be transformed manually or by using CCActions. See Actions for more information.

There is also a family of CCScene classes called transitions, implemented with the CCTransitionScene class. These allow you to create special transition effects when switching from one scene to another--for example, fading, sliding in from the side, and so on.

Director

The CCDirector is a shared (singleton) object that takes care of navigating between scenes. It knows which scene is currently active and allows you to change scenes by replacing the current scene or pushing a new one onto the scene stack. When you push a new scene onto the stack, the CCDirector pauses the previous scene but keeps it in memory. Later, when you pop the top scene from the stack, the paused scene resumes from its last state.

The CCDirector is also responsible for initializing OpenGL ES.

Layers

A CCLayer is a CCNode that knows how to handle touch events. Layers know how to draw themselves and may be semi-transparent, allowing players to see other layers behind them. CCLayers are very useful in defining your game's appearance and behavior, so you should expect to spend a lot of your programming time coding CCLayer subclasses that do what you need.

The CCLayer is where you define touch event handlers. By implementing a method to handle one of the touch events (ccTouchBegan, ccTouchMoved, ccTouchEnded, or ccTouchCancelled) a CCLayer can react to the player's interaction. These touch events are propagated to all the layers within a scene, from front to back, until some layer catches the event and accepts it.

While complex applications will require you to define custom CCLayer subclasses, cocos2d provides several predefined layers. Some examples include CCMenu (a simple menu layer), CCColorLayer (a layer that draws a solid color), and CCLayerMultiplex (a layer that lets you multiplex its children, activating one at a time while disabling the others).

Layers may contain any CCNode as a child, including CCSprites, CCLabels, and even other CCLayer objects. Because layers are a subclass of CCNode, they can be transformed manually or by using CCActions. See Actions for more information.

Multiple Layers Example:
  1. 1 CCLayerGradient* layer1 = CCLayerGradient::create(ccc4(255, 0, 0, 255), ccc4(255, 0, 255, 255));
  2. 2 layer1->setContentSize(CCSizeMake(80, 80));
  3. 3 layer1->setPosition(ccp(50,50));
  4. 4 addChild(layer1);
  5. 5
  6. 6 CCLayerGradient* layer2 = CCLayerGradient::create(ccc4(0, 0, 0, 127), ccc4(255, 255, 255, 127));
  7. 7 layer2->setContentSize(CCSizeMake(80, 80));
  8. 8 layer2->setPosition(ccp(100,90));
  9. 9 addChild(layer2);
  10. 10
  11. 11 CCLayerGradient* layer3 = CCLayerGradient::create();
  12. 12 layer3->setContentSize(CCSizeMake(80, 80));
  13. 13 layer3->setPosition(ccp(150,140));
  14. 14 layer3->setStartColor(ccc3(255, 0, 0));
  15. 15 layer3->setEndColor(ccc3(255, 0, 255));
  16. 16 layer3->setStartOpacity(255);
  17. 17 layer3->setEndOpacity(255);
  18. 18 ccBlendFunc blend;
  19. 19 blend.src = GL_SRC_ALPHA;
  20. 20 blend.dst = GL_ONE_MINUS_SRC_ALPHA;
  21. 21 layer3->setBlendFunc(blend);
  22. 22 addChild(layer3);

Sprites

A cocos2d CCSprite is similar to sprites you find in other game engines. It is a 2D image that can be moved, rotated, scaled, animated, and undergo other transformations. Sprites (implemented using the CCSprite class) can have other sprites as children. When a parent is transformed, all its children are transformed as well. Because sprites are a subclass of CCNode, they can be transformed manually or by using CCActions. See Actions for more information.

References

cocos2d for iPhone:cocos2d Basic Concepts

Director Scene Layer and Sprite的更多相关文章

  1. Cocos2d-x v3.1 核心类Director,Scene,Layer和Sprite(六)

    Cocos2d-x v3.1 核心类Director,Scene,Layer和Sprite(六) Scene就像一个舞台一样在上面会摆放各种的元素,有的是固定的比如说布景,道具都是固定不动的,但有的元 ...

  2. cocos2d基本类介绍 director/scene/layer/sprite

    [核心类]     导演Director.场景Scene.布景层Layer.精灵Sprite的概念请移步:     导演控制场景,场景控制图层,图层控制精灵,精灵控制动作.     相互之间的关系框架 ...

  3. cocos2dx[3.2](7) 核心类Director/Scene/Layer/Sprite

    [核心类] 导演Director.场景Scene.布景层Layer.精灵Sprite的概念请移步: cocos2dx基础篇(2) 第一个程序 导演控制场景,场景控制图层,图层控制精灵,精灵控制动作. ...

  4. arcgis api for JavaScript _加载三维图层(scene layer)

    arcgis api for JavaScript _加载三维图层(scene layer) arcgis api for JavaScript  4.x 版本增加对三维的支持. 关于三维图层(sce ...

  5. cocos2dx Scene,Layer,Sprite的理解

    layer,scene,sprite的默认锚点都是0.5,0.5 三者都继承自Node节点,暂时没看出有什么区别,或者下面的话是对的吧. 在cocos2d-x中,一个应用可以有多个scene,但任何时 ...

  6. 【深入Cocos2d-x】使用MVC架构搭建游戏Four

    喜欢Four这个项目,就赶快在GitHub上Star这个项目吧! 喜欢我的文章,来微博关注我吧:王选易在学C艹 点我下载 项目起源 项目Logo: 下面是该游戏的项目地址,各位想参考源代码的同学可以到 ...

  7. cocos2d-x开发记录:二,基本概念(导演,场景,层和精灵,场景切换,效果)

    四,Director Scene Layer和Sprite(导演,场景,层和精灵) 1.Scenes(场景) 一个场景 (用CCScene对象实现)相当于APP工作流的独立部分.一些人也喜欢叫做“屏幕 ...

  8. Cocos2d html5 笔记 1: overview

    昨天接触到了cocos2d-html5的的东东了, 第一次看其源代码一头雾水,幸好samples目录下面有几个例子,可以从这个入手. MoonWarriors是一个射击类的游戏, 有点像以前玩的雷电, ...

  9. 五毛的cocos2d-x学习笔记03-控件

    VS2013快捷键:注释,Ctrl+K+C:取消注释Ctrl+K+U.都是单行.要实现多行注释与取消注释,就选中多行.run方法调用了AppDelegate的applicationDidFinishL ...

随机推荐

  1. PHP 中的随机数——你觉得可靠么?

    本文主要分析以加密为目的的随机数生成问题.PHP 5 并未提供生成强加密随机数的简便机制,但是,PHP 7 引入了两个 CSPRNG 函数以解决该问题.系 OneAPM 工程师编译整理. 什么是 CS ...

  2. USB otg 学习笔记

    1 USB OTG的工作原理 OTG补充规范对USB2.0的最重要的扩展是其更具节能性的电源管理和允许设备以主机和外设两种形式工作.OTG有两种设备类型:两用OTG设备(Dualrole device ...

  3. bzoj1899

    显然如果只有一个窗口,是一道贪心的题目,直接让吃饭慢的排在前面即可 两个窗口的话,我们还是根据这个原则 先对吃饭时间降序排序,然后这是一个dp 假如设当前处理到第i个人,当在窗口1的打饭时间确定了,窗 ...

  4. React入门1

    React教程 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...

  5. 【转】 Android BCM4330 蓝牙BT驱动调试记录

    原文网址:http://blog.csdn.net/dwyane_zhang/article/details/7180779 网上关于BT的驱动很少,所以我在开发过程中把其中的步骤记录下来.供大家相互 ...

  6. oracle的基本信息查询

    oracle查看当前数据库实例名 select name from V$DATABASE; 或者 select SYS_CONTEXT('USERENV','INSTANCE_NAME') from ...

  7. Java---replace与replaceAll的区别

    乍一看,字面上理解好像replace只替换第一个出现的字符(受javascript的影响),replaceall替换所有的字符,其实大不然,只是替换的用途不一样. 这两者很容易搞混,在这里详细讲述下. ...

  8. 打开U盘里是U盘的快捷方式?(2013.12.05)

    昨天去打印室打印,结果U盘就中招了 症状:   我的U盘:                                                            打开变成了里面:   ...

  9. [LeetCode] 287. Find the Duplicate Number 解题思路

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  10. E. Three States - Codeforces Round #327 (Div. 2) 590C States(广搜)

    题目大意:有一个M*N的矩阵,在这个矩阵里面有三个王国,编号分别是123,想知道这三个王国连接起来最少需要再修多少路. 分析:首先求出来每个王国到所有能够到达点至少需要修建多少路,然后枚举所有点求出来 ...