- 浏览: 247015 次
- 性别:
- 来自: 天津
文章分类
最新评论
-
yulanlian:
...
实现在删除数据后,自增列的值连续 -
RonQi:
楼主写的很好,支持原创!
Google Protocol Buffers
cocos2d拥有很完美的菜单管理和场景切换类,你可以使用它们为你的游戏加入菜单,完成场景之间的切换。 这次我会简单的介绍一下如何使用cocos2d提供的类库为游戏加入漂亮的菜单场景。在这一课你将会接触到菜单项的处理和场景的切换等相关知识。我不会讲述大篇幅的技术理论,我认为太多的理论对于一个初学者不仅没有帮助,反而会让他失去学习的乐趣。个人认为从实例代码下手更好,经过多次的实践之后,让读者自己去参悟其中的理论会更有效。 抛开题外话,让我们继续本章的主题。首先,让我们来创建我们的菜单类,这个类将包含两个部分,菜单场景类和菜单层类。因此我们在工程classes文件中建立一个New Group来存放管理他们会更方便。右击classes类文件夹,在弹出的菜单项中选择Add─>New Group创建一个新的Group,并将他命名为Menu。然后右击Menu文件夹,在弹出的菜单项中选择Add─>New File依次创建两个基于CCLayer的类文件,分别将它们命名为MenuScene和MenuLayer。这时你的Menu文件夹下应该有4个文件,分别是:MenuScene.h,MenuScene.h ,MenuLayer.h和MenuLayer.m文件。下面直接贴代码了: MenuScene.h中的代码如下: #import <Foundation/Foundation.h> #import "cocos2d.h" @interface MenuScene : CCLayer { } +(id) scene; @end MenuScene.m中的代码如下: #import "MenuScene.h" #import "MenuLayer.h" @implementation MenuScene // 实例化本场景 +(id) scene { // 'scene' is an autorelease object. CCScene *scene = [CCScene node]; MenuScene *layer = [MenuScene node]; [scene addChild: layer]; return scene; } // 初始化函数 - (id) init { self = [super init]; if (self) { // 设定菜单背景 CGSize winSize = [[CCDirector sharedDirector] winSize]; CCSprite *bg = [CCSprite spriteWithFile:@"MenuBackground.png"]; bg.position = ccp(winSize.width / 2, winSize.height / 2); [self addChild:bg]; // 添加菜单层 [self addChild:[MenuLayer node]]; } return self; } // on "dealloc" you need to release all your retained objects - (void) dealloc { [super dealloc]; } @end
#import <Foundation/Foundation.h> #import "cocos2d.h" @interface MenuLayer : CCLayer { } // 下面声明的三个方法是菜单的三个选项,开始游戏,分数和关于 - (void) startGame: (id)sender; - (void) scores: (id)sender; - (void) onAbout: (id)sender; @end #import "HelloWorldScene.h" #import "MenuLayer.h" #import "ScoreScene.h" #import "AboutScene.h" @implementation MenuLayer // on "init" you need to initialize your instance - (id) init { self = [super init]; if (self) { // 设定菜单文字 [CCMenuItemFont setFontName:@"Helvetica"]; [CCMenuItemFont setFontSize:30]; CCMenuItem *start = [CCMenuItemFont itemFromString:@"Start Game" target:self selector:@selector(startGame:)]; CCMenuItem *scores = [CCMenuItemFont itemFromString:@"Scores" target:self selector:@selector(scores:)]; CCMenuItem *abouts = [CCMenuItemFont itemFromString:@"About" target:self selector:@selector(onAbout:)]; CCMenu *menu = [CCMenu menuWithItems:start, scores, abouts, nil]; [menu alignItemsVertically]; // 菜单特效可有可无,实现一个菜单飞出式效果 CGSize s = [[CCDirector sharedDirector] winSize]; int i=0; for( CCNode *child in [menu children] ) { CGPoint dstPoint = child.position; int offset = s.width/2 + 50; if( i % 2 == 0) offset = -offset; child.position = ccp( dstPoint.x + offset, dstPoint.y); [child runAction:[CCEaseElasticOut actionWithAction:[CCMoveBy actionWithDuration:2 position:ccp(dstPoint.x- offset,0)]period: 0.35f]]; i++; } [self addChild:menu]; } return self; } // 实现菜单方法 // 切换到StartGame场景 - (void) startGame: (id)sender { CCScene *sc = [CCScene node]; [sc addChild:[HelloWorld node]]; // 缩放的形式切换场景 [[CCDirector sharedDirector] replaceScene: [CCShrinkGrowTransition transitionWithDuration:1.2f scene:sc]]; } // 切换到Scores场景 - (void) scores: (id)sender { CCScene *sc = [CCScene node]; [sc addChild:[ScoreScene node]]; // 缩放的形式切换场景 [[CCDirector sharedDirector] replaceScene: [CCShrinkGrowTransition transitionWithDuration:1.2f scene:sc]]; } // 切换到About场景 - (void) onAbout: (id)sender { CCScene *sc = [CCScene node]; [sc addChild:[AboutScene node]]; // 缩放的形式切换场景 [[CCDirector sharedDirector] replaceScene: [CCShrinkGrowTransition transitionWithDuration:1.2f scene:sc]]; } // on "dealloc" you need to release all your retained objects - (void) dealloc { [super dealloc]; } @end 到这里菜单类的编写就结束了,这里运用了场景和层分离的概念。将菜单场景与菜单层分开实现,这样可以使代码更加的清晰,层次更加分明。但是现在你还无法成功的运行你的程序,因为还有菜单项没用实现。另外你还没有修改Delegate.m托管文件中首场景载入函数的参数,你需要让它指向你编写的菜单场景。下面我们就来完成剩下的工作。首先,用同样的方法在classes文件夹下创建名为MenuOption的组,并在其下创建两个基于CCLayer的类文件,分别命名为AboutScene和ScoreScene。然后打开Delegate.m托管文件,将头文件"HelloWorldScene.h"改为"MenuScene.h"。再在- (void) applicationDidFinishLaunching:(UIApplication*)application 方法的最后一行找到 [[CCDirector sharedDirector] runWithScene: [HelloWorld scene]]; 这段函数调用,将参数中的HelloWorld改为刚创建的MenuScene场景。下面贴出菜单项类的代码: HelloWorldScene.h中的代码如下: #import <Foundation/Foundation.h> #import "cocos2d.h" // HelloWorld Layer @interface HelloWorld : CCLayer { } @end HelloWorldScene.m中的代码如下: #import "HelloWorldScene.h" #import "MenuScene.h" // HelloWorld implementation @implementation HelloWorld // on "init" you need to initialize your instance -(id) init { // always call "super" init // Apple recommends to re-assign "self" with the "super" return value if( (self=[super init] )) { // create and initialize a Label CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64]; // ask director the the window size CGSize size = [[CCDirector sharedDirector] winSize]; // position the label on the center of the screen label.position = ccp( size.width /2 , size.height/2 ); // add the label as a child to this Layer [self addChild: label]; // 添加图形返回菜单 CCSprite *menuNormal = [CCSprite spriteWithFile:@"b_back.png" rect:CGRectMake(0, 0, 78, 41)]; CCSprite *menuSelected = [CCSprite spriteWithFile:@"b_back_s.png" rect:CGRectMake(0, 0, 78, 41)]; CCMenuItemSprite *backToMenu = [CCMenuItemSprite itemFromNormalSprite:menuNormalselectedSprite:menuSelected target:self selector:@selector(onBack:)]; CCMenu *menu = [CCMenu menuWithItems: backToMenu, nil]; [menu setPosition:ccp(480 - 50, 320 - 30)]; [self addChild: menu]; } return self; } // 返回菜单方法 -(void) onBack: (id) sender { CCScene *sc = [CCScene node]; [sc addChild:[MenuScene node]]; [[CCDirector sharedDirector] replaceScene: [CCSlideInRTransition transitionWithDuration:1.2f scene:sc]]; } // on "dealloc" you need to release all your retained objects - (void) dealloc { // in case you have something to dealloc, do it in this method // in this particular example nothing needs to be released. // cocos2d will automatically release all the children (Label) // don't forget to call "super dealloc" [super dealloc]; } @end AboutScene.h中的代码如下: #import <Foundation/Foundation.h> #import "cocos2d.h" @interface AboutScene : CCLayer { } @end #import "AboutScene.h" #import "MenuScene.h" @implementation AboutScene // on "init" you need to initialize your instance -(id) init { if( (self=[super init] )) { // 用Label显示作者 CCLabel* label = [CCLabel labelWithString:@"作者:谢映宇" fontName:@"Marker Felt" fontSize:40]; CGSize size = [[CCDirector sharedDirector] winSize]; label.position = ccp( size.width /2 , size.height/2 ); [self addChild: label]; // 添加返回菜单 [CCMenuItemFont setFontSize:20]; CCMenuItem *backToMenu = [CCMenuItemFont itemFromString:@"Back" target:selfselector:@selector(onBack:)]; CCMenu *mn = [CCMenu menuWithItems:backToMenu, nil]; [mn alignItemsVertically]; mn.position = ccp (480 - 50, 30); [self addChild:mn z:1 tag:2]; } return self; } // 返回菜单方法 -(void) onBack: (id) sender { CCScene *sc = [CCScene node]; [sc addChild:[MenuScene node]]; [[CCDirector sharedDirector] replaceScene: [CCSlideInRTransition transitionWithDuration:1.2f scene:sc]]; } // on "dealloc" you need to release all your retained objects - (void) dealloc { [super dealloc]; } @end #import <Foundation/Foundation.h> #import "cocos2d.h" @interface ScoreScene : CCLayer { } @end #import "ScoreScene.h" #import "MenuScene.h" @implementation ScoreScene // on "init" you need to initialize your instance -(id) init { if( (self=[super init] )) { // 用Label显示分数 CCLabel* label = [CCLabel labelWithString:@"最高分:9999" fontName:@"Marker Felt" fontSize:40]; CGSize size = [[CCDirector sharedDirector] winSize]; label.position = ccp( size.width /2 , size.height/2 ); [self addChild: label]; // 添加返回菜单 [CCMenuItemFont setFontSize:20]; CCMenuItem *backToMenu = [CCMenuItemFont itemFromString:@"Back" target:selfselector:@selector(onBack:)]; CCMenu *mn = [CCMenu menuWithItems:backToMenu, nil]; [mn alignItemsVertically]; mn.position = ccp (480 - 50, 30); [self addChild:mn z:1 tag:2]; } return self; } // 返回菜单方法 -(void) onBack: (id) sender { CCScene *sc = [CCScene node]; [sc addChild:[MenuScene node]]; [[CCDirector sharedDirector] replaceScene: [CCSlideInRTransition transitionWithDuration:1.2f scene:sc]]; } // on "dealloc" you need to release all your retained objects - (void) dealloc { [super dealloc]; } @end
发表评论
-
ios 国际化相关
2011-10-08 18:49 1471添加国际化 1. 代码中使用字符串的地方 使用 NS ... -
iPhone开发笔记
2011-09-26 21:52 1117退回输入键盘: - (BOOL) textFie ... -
ios开发基础知识 - 1
2011-09-26 21:51 1149输出 command+shift+r NSLog(@& ... -
ios---超链接的UILabel教程
2011-09-26 21:50 1560超链接的UILabel教程 -
ios开发基础问题总结
2011-09-26 21:49 1294订阅 1,如何进入应用后首 ... -
UILabel用法
2011-09-26 20:45 1840/设置换行 UILabel*label; / ... -
IPhone之UIScrollView [转载]
2011-09-22 15:55 936原文地址:IPhone之UIScrollView作者:飞 ... -
手势识别
2011-09-03 17:19 908注册: UISwipeGestureReco ... -
“碰撞”探测
2011-08-21 14:28 864首先要向大家说明的是,所谓“碰撞”探测没有大家想想 ... -
coco2d地图显示相关类
2011-08-21 07:55 1635摘自:知易教程 Cocos2d-iPhone 实现地图 ... -
Cocos2D 的事件处理机制
2011-08-21 07:21 1631摘自:知易教程 系统当前场景对象包含多个层 ... -
cocos2d游戏开发,常用工具集合
2011-08-20 19:40 844位图字体工具Bitmap Font ToolsBMFon ... -
iPhone OS 的 Touche 事件
2011-08-20 19:34 1359知易教程 基础知识 在开始介绍 iPhone OS ... -
iPad, iPhone, and Aspect Ratio
2011-08-05 11:58 971好,现在处理iphone上 ... -
Retina Display and Cocos2D
2011-08-05 11:57 1356最新版本的cocos2d完全 ... -
网络编程总结
2011-08-04 15:41 838一:确认网络环境3G/WIFI 1. 添加源文件和fra ... -
Cocos2D界面切换方式
2011-08-04 15:37 1647CCTransitionFade, //渐隐 ... -
Protocol协议的用法
2011-08-04 15:30 1271一、说明 两个类进行通讯,用协议就比较方便。 (书 ... -
IOS开源项目和官方例子汇总
2011-08-04 14:22 1861开源项目: 扫描wifi信息:http://code. ... -
cocos2d场景和UIViewController视图的切换
2011-08-04 14:20 1630cocos2d中从场景切换到UIViewController视 ...
相关推荐
在Cocos2d-x游戏开发中,场景(Scene)是游戏世界的基本构建单元,它代表了一个独立的游戏状态或者说是屏幕上的一个可视...通过实际操作和调试,你将对Cocos2d-x的场景管理有更深入的理解,为游戏开发打下坚实基础。
此外,cocos2d-x提供了丰富的图形渲染功能,如精灵、层、场景、动作等,方便开发者构建各种游戏元素。在游戏逻辑层面,cocos2d-x的事件处理系统和定时器使得游戏逻辑的编写变得简单易懂。 闯关类游戏通常包含多个...
例如,我们可以在主菜单场景中创建一个MenuItem,当用户点击时,触发相应的事件处理函数,切换到新的游戏场景。 此外,cocos2d-x的事件系统使得菜单的交互变得非常灵活。通过`EventListenerTouchOneByOne`,我们...
在游戏开发领域,cocos2d是一个广泛使用的2D游戏引擎,尤其适合初学者入门。本篇将深入探讨如何利用cocos2d构建一款塔防游戏,从基础概念到实战技巧,全方位解析这一过程。 一、cocos2d简介 cocos2d是一款开源的2D...
本文将详细讲解如何使用Cocos2d-x框架来设计和实现这样的功能,以《赵云要格斗》这个游戏为例。 Cocos2d-x是一个流行的开源跨平台2D游戏开发框架,它基于C++,支持多种操作系统,包括iOS、Android和Windows等。在...
3. `lua`:Lua脚本文件夹,cocos2d-x支持使用Lua作为脚本语言,提供灵活的游戏逻辑实现。 4. `proj.android` / `proj.ios_mac`:针对不同平台的构建项目,用于编译和打包应用。 二、cocos2d-x基础组件 1. `Scene`...
5. **游戏场景和层级**:Cocos2d-X使用场景(Scene)和层级(Layer)的概念来组织游戏的视觉元素。场景是游戏的顶级容器,而层级则在场景内部管理不同的游戏组件,如菜单、游戏界面、动画效果等。 6. **UI设计**:...
在这个实例中,开发者将深入学习如何利用Cocos2d-x构建一个完整的游戏场景,包括游戏主菜单、游戏关卡、角色控制、敌人生成、子弹系统、碰撞检测以及得分系统等多个关键组成部分。 首先,游戏主菜单的设计是游戏的...
在iOS游戏开发中,Cocos2D是一个非常流行的2D游戏引擎,它为开发者提供了丰富的功能,包括场景管理、精灵、动作、物理引擎等。在这个“cocos2d 游戏菜单设置v1”项目中,我们将探讨如何利用Cocos2D来创建游戏菜单,...
9. **游戏存档**:通过源码,你可以了解如何使用Cocos2d-Js保存和读取游戏进度,例如使用JSON或本地存储。 10. **性能优化**:书中的源码可能包含了性能优化技巧,如减少不必要的绘制、合理使用缓存、避免内存泄漏...
总结起来,“愤怒的小鱼”这款游戏案例充分展示了cocos2d在2D游戏开发中的强大功能,包括场景、层、精灵的组织结构,动画和动作的实现,物理引擎的应用,事件处理机制,以及音频管理和脚本支持。通过对这个游戏的...
《Cocos2d-x游戏引擎实战开发炸弹超人》是一个基于Cocos2d-x框架的2D游戏开发教程,旨在帮助开发者深入理解并熟练运用这一强大的游戏引擎。Cocos2d-x是一个开源、跨平台的2D游戏开发工具,它支持iOS、Android、...
本文将深入探讨如何使用Cocos2d-x来实现"RunOrDie"这一款小游戏,旨在帮助读者理解Cocos2d-x的基本工作原理以及游戏开发流程。 一、Cocos2d-x简介 Cocos2d-x是Cocos2d家族的一员,它具有跨平台的优势,可以在iOS、...
这个"跑酷(StarterKit)游戏源码"是基于Cocos2d-x框架构建的一个Android游戏项目,旨在为开发者提供一个基础的跑酷类游戏模板,帮助他们快速入门游戏开发。 在分析这个源码时,我们可以学习到以下关键知识点: 1....
1. **场景管理**:在Cocos2d-X中,游戏逻辑通常被组织为不同的“Scene”(场景),例如游戏主界面、游戏进行中的棋盘界面、胜利或失败的结果界面。开发者需要创建并管理这些场景之间的切换,确保游戏流程的顺畅。 2...
《迷失航线》是一款基于Cocos2d-x框架开发的射击类游戏,专为移动设备设计。Cocos2d-x是一个开源的游戏开发框架,它使用C++作为主要编程语言,同时支持Lua和JavaScript,广泛应用于2D游戏开发。在这个项目实战中,...
通过这个cocos2d-x的小demo,开发者不仅可以了解cocos2d-x的基本使用,还能掌握如何用它来构建一个完整的游戏,包括游戏逻辑、界面设计、交互实现等多个方面。对于初学者来说,这是一个很好的实践项目,有助于加深对...
在cocos2d-x中实现“俄罗斯方块”,首要任务是创建游戏场景和游戏对象。我们可以使用cocos2d-x的精灵(Sprite)类来表示方块,每个单元块可以是一个单独的精灵。然后,我们需要一个游戏板(Grid)类来管理屏幕上的...