cocos2D(六)----CCLayer
一个游戏中能够有非常多个场景,每一个场景里面又可能包括有多个图层,这里的图层一般就是CCLayer对象。CCLayer本身差点儿没什么功能。对照CCNode,CCLayer可用于接收触摸和加速计输入。事实上。cocos2d对图层并没有严格的要求,图层不一定要使用CCLayer类,它也能够是一个简单的CCNode。为什么呢?我们新建一个图层不就是为了能够容纳很多其它的子节点么,CCNode也能够加入子节点啊。所以。假设你的图层不须要接收触摸和加速计输入,就尽量使用CCNode表示图层,CCLayer由于能够接收触摸和加速计输入会添加不必要的开销。移动、缩放、旋转整个图层,图层上的全部节点也会跟着一起移动、缩放、旋转。
经常使用设置
1.接收触摸输入
CCLayer默认情况是不接收触摸输入的,须要显示地设置isTouchEnabled为YES
- self.isTouchEnabled = YES;
设置isTouchEnabled为YES后,就会调用图层对应的方法来处理触摸输入:
这些都是在CCStandardTouchDelegate协议中定义的方法
1> 当单指接触到屏幕时
- - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
2> 当手指在屏幕上移动时
- - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
3> 当单指离开屏幕时
- - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
4> 当触摸被取消时
- - (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
非常少会发生触摸被取消的情况,所以大多数情况下可忽略,或用ccTouchesEnded取代,由于ccTouchesCancelled和ccTouchesEnded类似
大部分情况下,我们须要知道触摸发生在什么位置。这里的触摸事件是由UIKit框架接收的,因此须要把触摸位置转换为OpenGL坐标。
比方在手指移动过程中:
- - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
- // 获取触摸对象
- UITouch *touch = [touches anyObject];
- // 获取触摸在UIView视图上的位置
- CGPoint uiPoint = [touch locationInView:touch.view];
- // 转换为OpenGL坐标
- CGPoint glPoint = [[CCDirector sharedDirector] convertToGL:uiPoint];
- }
以下利用一个小样例来综合使用上述的方法。如果图层上有个精灵,我手指触摸到哪,这个精灵的位置就在哪
首先在图层初始化的时候加入精灵
- // 图层的init方法
- -(id) init
- {
- if( (self=[super init])) {
- // 初始化一个精灵
- CCSprite *lufy = [CCSprite spriteWithFile:@"lufy.png"];
- CGSize size = [[CCDirector sharedDirector] winSize];
- lufy.position = ccp(size.width * 0.5f, size.height * 0.5f);
- // 加入精灵。并设置标记
- [self addChild: lufy z:0 tag:kLufyTag];
- self.isTouchEnabled = YES;
- }
- return self;
- }

接下来是在图层中接收触摸输入
- // 计算触摸在图层中的位置(OpenGL坐标)
- - (CGPoint)locationInLayer:(NSSet *)touches {
- // 获取触摸对象
- UITouch *touch = [touches anyObject];
- // 获取触摸在UIView视图上的位置
- CGPoint uiPoint = [touch locationInView:touch.view];
- // 转换为OpenGL坐标
- CGPoint glPoint = [[CCDirector sharedDirector] convertToGL:uiPoint];
- return glPoint;
- }
- // 因为ccTouchesBegan、ccTouchesMoved、ccTouchesEnded中的做法都是一样,所以抽成一个方法
- - (void)dealTouches:(NSSet *)touches {
- // 计算触摸的位置
- CGPoint point = [self locationInLayer:touches];
- // 依据标记获取精灵
- CCSprite *lufy = (CCSprite *)[self getChildByTag:kLufyTag];
- // 设置精灵的位置
- lufy.position = point;
- }
- - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- [self dealTouches:touches];
- }
- - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
- [self dealTouches:touches];
- }
- - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
- [self dealTouches:touches];
- }
图层的触摸输入临时说到这里,其它高级的使用方法在后面会提及
2.接收加速计输入
CCLayer默认情况是不接收加速计输入的,须要显示地设置isAccelerometerEnabled为YES
- self.isAccelerometerEnabled = YES;
设置isAccelerometerEnabled为YES后,就会调用图层对应的方法来处理加速计输入:
这是在UIAccelerometerDelegate协议中定义的方法
- - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
- // typedef double UIAccelerationValue;
- UIAccelerationValue x = acceleration.x;
- UIAccelerationValue y = acceleration.y;
- UIAccelerationValue z = acceleration.z;
- // x,y,z代表三维中随意方向的加速度
- }
CCLayerColor
有时候,我们想给整个图层设置一种背景颜色,那么就须要用到CCLayerColor了。CCLayerColor是CCLayer的子类
- // 红色:#ffff0000
- ccColor4B color = ccc4(255, 0, 0, 255);
- // 初始化一个颜色图层
- CCLayerColor *layerColor = [CCLayerColor layerWithColor:color];
- // 加入到场景中
- [scene addChild:layerColor];
效果图:

CCLayerGradient
CCLayerGradient是CCLayerColor的子类,能够给图层设置渐变色
- // 红色:#ffff0000
- ccColor4B red = ccc4(255, 0, 0, 255);
- // 蓝色:#ff0000ff
- ccColor4B blue = ccc4(0, 0, 255, 255);
- // 初始化一个渐变图层,从红色渐变到蓝色
- CCLayerGradient *layerGradient = [CCLayerGradient layerWithColor:red fadingTo:blue];
- // 加入到场景中
- [scene addChild:layerGradient];
效果图:

CCLayerMultiplex
CCLayerMultiplex继承自CCLayer。称为"多重图层"。它能够包括多个CCLayer对象,但在随意时刻仅仅能够有一个CCLayer处于活动状态,用switchTo:和switchToAndReleaseMe:方法能够让某个图层处于活动状态。差别在于switchToAndReleaseMe:方法会先释放当前处于活动状态的图层,再让參数中要求的图层处于活动状态
- // 创建2个图层
- CCLayer *layer1 = [CCLayer node];
- CCLayer *layer2 = [CCLayer node];
- // 创建一个多重图层,包括了layer1和layer2
- CCLayerMultiplex *plex = [CCLayerMultiplex layerWithLayers:layer1, layer2, nil];
- // 让layer1处于活动状态(layer2还在内存中)
- [plex switchTo:0];
- // 让layer2处于活动状态(layer1还在内存中)
- [plex switchTo:1];
- // 释放当前处于活动状态的layer2(layer2从内存中移除),然后让layer1处于活动状态
- [plex switchToAndReleaseMe:0];
图层之间的切换是没有过渡效果的
原文地址:http://blog.csdn.net/q199109106q/article/details/8601533
感谢作者~!
cocos2D(六)----CCLayer的更多相关文章
- Cocos2d-android (01) 创建一个简单的cocos2d应用程序
下载Cocos2d-android的源代码:cocos2d-android-1 git@github.com:ZhouWeikuan/cocos2d.git 将项目导入到eclipse中.运行实例: ...
- 基于cocos2d开发的android小游戏——採花仙
/*cocos 2d 已经成为了如今移动端游戏开发的强有力的工具,眼下主流游戏中多採用cocos 2d游戏引擎. 我也尝试了一下该引擎.我是用的是cocos2d-android,以后要移植到Cocos ...
- CosCos2D-android 代码总结
CosCos2D-android 学习总结 资料: Android游戏开发视频教程 Cocos-android-1代码包下载 直接上代码: MainActivity中Cocos常规写法: //coco ...
- Cocos2d-android (06) 屏幕触摸事件及坐标转换
为屏幕添加触摸事件,将左上角坐标转换为左下角坐标 package com.arlen.cocos2d.touch01; import org.cocos2d.layers.CCLayer; impor ...
- Cocos2d-android (05) 渐变动画(颜色,淡入淡出。。。)
淡入淡出.颜色渐变及动作重复执行 import org.cocos2d.actions.base.CCRepeatForever; import org.cocos2d.actions.interva ...
- Cocos2d-android (04) 执行多个动作
先后.同时执行多个动作及动作序列执行结束后的事件 import org.cocos2d.actions.instant.CCCallFunc; import org.cocos2d.actions.i ...
- Cocos2d-android (03) 向量
向量的基本运算及动作 import org.cocos2d.actions.interval.CCJumpBy; import org.cocos2d.actions.interval.CCMoveB ...
- Cocos2d-android (02) 添加一个精灵对象
什么是精灵: 1.精灵就是游戏当中的一个元素,通常用于代表画面当前中的一个事物,例如主人公,NPC和背景元素等: 2.一个精灵对象通常都与一张图片关联 3.精灵对象可以通过动作对象(CCAction) ...
- cocos2d_android 瞬间动作
该文章所写的瞬间动作主要有CCFlipX,CCFlipY,CCHide,CCShow 当中CCFlipX是以Y轴为中心旋转,CCFlipY是以X轴为中心旋转,CCHide将精灵对象隐藏,CCShow将 ...
- Cocos2d-x 学习之引擎介绍
Cocos2d-X是一个开源的移动2D游戏框架,MIT许可证下发布的.这是一个C + +cocos2d-iPhone项目的版本.cocos2d-X发展的重点是围绕cocos2d跨平台.即其实现一次编码 ...
随机推荐
- Android+Jquery Mobile学习系列(1)-开发环境
开发环境是老生常谈的问题了,网上有很多关于Android环境安装的文章,我这里也就简单说明一下,不做过多分析. 想了解详细的安装说明,可以参见[百度经验] Java环境安装直接跳过,说一下Androi ...
- 第17章 Redis概述
17.2.1 在Windows下安装Redis https://github.com/ServiceStack/redis-windows/tree/master/downloads redis-se ...
- C++数字图像处理(1)-伽马变换
https://blog.csdn.net/huqiang_823/article/details/80767019 1.算法原理 伽马变换(幂律变换)是常用的灰度变换,是一种简单的图像增强算法 ...
- C# 获取操作系统相关信息
1.获取操作系统版本(PC,PDA均支持) Environment.OSVersion 2.获取应用程序当前目录(PC支持) Environment.CurrentDirectory 3.列举本地硬盘 ...
- maven nexus memory optimization
#链接地址:https://help.sonatype.com/repomanager3/system-requirements#filehandles While starting Nexus I ...
- 智能家居控制APPUI界面设计
2017年,随着智能化产业进入新的市场格局,千家品牌实验室也迎来全新的升级,致力为智能产业生态链提供更全更新更深度的行业分析和品牌数据监测服务.本文为大家带来关于中国智能家居行业发展APP设计欣赏. ...
- Android媒体解码MediaCodec,MediaExtractor
Android提供了MediaPlayer播放器播放媒体文件,其实MediaPlyer只是对Android Media包下的MediaCodec和MediaExtractor进行了包装,方便使用.但是 ...
- Docker的官网在线--中文教程
1.官网界面:https://www.docker.com/tryit/ In this 10-minute tutorial, see how Docker works first-hand: Yo ...
- 常用shell备份脚本
#!/bin/sh # File: /路径/mysql/backup_mydb.sh # Database info DB_NAME="szby" DB_USER="ro ...
- 序列模型(5)-----双向神经网络(BRNN)和深层循环神经网络(Deep RNN)
一.双向循环神经网络BRNN 采用BRNN原因: 双向RNN,即可以从过去的时间点获取记忆,又可以从未来的时间点获取信息.为什么要获取未来的信息呢? 判断下面句子中Teddy是否是人名,如果只从前面两 ...