logo资料库

传智播客cocos2dx课程文档.doc

第1页 / 共578页
第2页 / 共578页
第3页 / 共578页
第4页 / 共578页
第5页 / 共578页
第6页 / 共578页
第7页 / 共578页
第8页 / 共578页
资料共578页,剩余部分请下载后查看
环境搭建
Cocos2dx搭建
android开发环境搭建
android cocos开发环境搭建
打开cocos2dx工程来编译
Cocos2D-x基础
什么是框架
Hello world 程序详解
选择器
精灵的创建
参考:cocos2d-x-win32初始化
参考:
三种模式
kResolutionUnKnown 认识 FrameSize
kResolutionExactFit and kResolutionShowAll 认识 WinS
kResolutionNoBorder 了解 VisibleSize 与 VisibleOrigin
kResolutionLeafsoar
开始基于 LsSize 的游戏设计实现
屏幕适配新解
=Hello调用过程图
Hello 的渲染树
Cocos2d核心类
CCNode 结点
继承结构
结构图:
主要成员数据
类函数
节点的创建与初始化
获取调试信息
加入节点
坐标体系
ZORDER和Tag
改变位置
设置缩放
扭曲效果系数
设置Z坐标
锚点
节点的尺寸
显示与隐藏
旋转
到达的顺序
OpenGL server状态
加节点获取节点
坐标转换
CCDirector导演类
继承图
成员数据
函数
创建导演
进入后台,暂停,继续,结束
CCScene 场景
继承图
创建场景
场景切换
场景切换的特效
代码实列
僵尸跳;
翻跳
翻页
CCLayer 层景
继承图
子类
子类的功能
触屏基础
分析触控事件的流程
触控处理以及代理设计模式
多个层同时注册触摸和多个触摸消息处理
触控事件小结
CCLayer
主要函数
CCLayerColor
CCLayerGradient
CCLayerMultiplex
CCMenu
CCMenu基础
CCControlSlider
文字显示
CCLabelTTF
CCLabelAtlas
批处理精灵
修改CCSprite实现触控
C++中的四个Cast
Cast与CCLableBMFont
文本输入与九宫格
CCTextFieldTTF
CCEditBox
设计分辨率
精灵波浪特效
Schedule和定时器
Selector小结
菜单持续激活的实现
声音和音效
OnEnter和onExit
内存管理
switchControl
代码测试
动作-Action
(移动)MoveTo / MoveBy
(缩放)ScaleTo / ScaleBy
(偏斜)SkewTo / SkewBy
(旋转)RotateTo / RotateBy
(旋转偏斜)RotationalSkewTo / RotationalSkewBy
(偏斜与旋转偏斜)Skew Comparison
(延时动作对比)Skew + Rotate + Scale
(跳) JumpTo / JumpBy
(贝塞尔曲线)BezierBy / BezierTo
(闪烁)Blink
(渐隐)FadeIn / FadeOut
(着色)TintTo / TintBy
(动画)Animation
(序列 组合动画)Sequence: Move + Rotate
(序列 组合动画-及时动画)Sequence of InstantActions
(序列 组合动画 延时动画 函数调用)callfunc
(序列 函数调用 节点 带数据)
(创建延时动作组合)CCSpawn
(重复动画)CallFuncN + RepeatForever
(重复与一直重复)Repeat/RepeatForever + RotateTo
(旋转颤动)RepeatForever / Repeat + Rotate
(反向动作)Reverse an action
(延时动作)DelayTime: m + delay + m
(可视切换,隐藏,组合动作)Reverse sequence 2
(重复动作与一直重复)Repeat / RepeatForever actions
(轨道 相机)OrbitCamera action
(跟随 画多边形)Follow action
(触发其他节点关联动作)ActionTargeted
(触屏)Tap screen
(附加动作)tacked Move + Jump actions
(附加动作)Stacked Move + Bezier actions
(附加动作移动+样条)
(多个动作同时)
(增加精灵与调用动作)
(精灵增加动作)addAction
(移动与反向移动 )
(数据传递)CCCallFuncND
(重复+移动)
(动作log)CCCallFuncN Rotation
(CatmullRom样条)CatmullRomBy / CatmullRomTo
(样条)CardinalSplineBy / CardinalSplineTo
(暂停/恢复)PauseResumeActions
(组合动画)Sequence: Move + Rotate + Scale + RemoveSelf
加速器Accelerometer
(加速传感测试)
管理类-ActionManager
(进入下一场景)
(逻辑性测试)Logic
(暂停/恢复)
(移动)
(恢复与定时器)
缓冲动作-ActionsEase
(基本缓冲动作)EaseIn - EaseOut – Stop
(开始结束都缓冲)EaseInOut and rates
(指数缓冲动作)ExpIn - ExpOut actions
(指数缓冲动作-开始结束都缓冲)EaseExponentialInOut action
(正弦缓冲)EaseSineIn – EaseSineOut
(正弦缓冲-开始结束都缓冲)EaseSineInOut action
(弹性缓冲)Elastic In - Out actions
(弹性缓冲动作-开始结束都缓冲)EaseElasticInOut action
(跳跃缓冲)Bounce In - Out actions
(跳跃缓冲-开始结束都缓冲)EaseBounceInOut action
(回震缓冲)Back In - Out actions
(回震缓冲-开始结束缓冲)EaseBackInOut action
(可变速动作)Speed action
进度动作-ActionProgress
(进度条定时器-半径样式-扇形)ProgressTo Radial
(进度条定时器-条状样式-水平)ProgressTo Horizontal
(进度条定时器-条状样式-垂直)ProgressTo Vertical
(设置中点-半径样式)Radial w/ Different Midpoints
(设置中点-条状样式)ProgressTo Bar Mid
(进度条+褪色)
(进度条定时器+精灵帧)Progress With Sprite Frame
物理引擎-Box2D
物理引擎-Box2DT-Bed
物理引擎-Chipmunk
点击+移动-ChickAndMove
(点击触控+背景变色)
声音引擎-CocosDenshion
(命名空间)
(加载音乐资源)
(设置声音大小)
(背景音乐)
(特效)
(调节音量)
配置-Configuration
(加载显示配置信息)Loading config file manually. See consol
(获取字符串信息)Using getCString(). Check the console
(加载无效的配置信息)Loading an invalid config file
(默认值)Tests defaults values
(增加信息配置信息)Tests setting values manually
Curl语言-Curl
Libcurl
当前语言-CurrentLanguage
(检测当前语言)
数据的访问者模式-DataVisitor
DataVisitor测试
简单的绘画-DrawPrimitive
基本物件-draw primitives
节点绘制Testing DrawNode - batched draws. Concave poly
特效-Effects
(场景的初始化)
(摇晃)Shaky3D
(波浪3D)Waves3D
(翻转)FlipX3D
(翻转)FlipY3D
(镜头)Lens3D
(涟漪)Ripple3D
(液体)Liquid
(波浪)Waves
(旋转)Twirl
(摇摆旋转)ShakyTiles3D
(破碎瓦片)ShatteredTiles3D
(洗牌)ShuffleTiles
(渐隐-左下角开始)FadeOutTRTiles
(渐隐-右上角开始)FadeOutBLTiles
(渐隐-向上)FadeOutUpTiles
(渐隐-向下)FadeOutDownTiles
(关闭)TurnOffTiles
(波浪地图)WavesTiles3D
(跳跃地图)JumpTiles3D
(行分离)SplitRows
(列分离)SplitCols
(翻页)PageTurn3D
高级特效-EffectsAdvanced
扩展-Extensions
文件工具包-FileUtils
(搜索文件)FileUtils: resolutions in directories
(搜索路径)FileUtils: search path
(文件查找)FileUtils: filename lookup
(检查文件是否存在)FileUtils: check whether the file exists
(将字典文件写入plist)FileUtils: CCDictionary to plist
字体Font
(字体显示)
fonts/A Damn Mess.ttf
fonts/Abberancy.ttf
fonts/Abduction.ttf
fonts/Paint Boy.ttf
fonts/Schwarzwald Regular.ttf
fonts/Scissor Cuts.ttf
(间隔时间)Interval
(定时器间隔测试)
键盘-keypad
标签-Label
LabelAtlas
LabelAtlasColor
Atlas3
Atlas4
Atlas5
Atlas6
AtlasBitmapColor
AtlasFastBitmap
BitmapFontMultiLine
LabelsEmpty
LabelBMFontHD
LabelAtlasHD
LabelGlyphDesigner
Atlas1
LabelTTFTest
LabelTTFMultiline
LabelTTFChinese
LabelBMFontChinese
BitmapFontMultiLineAlignment
LabelTTFA8Test
BMFontOneAtlas
BMFontUnicode
BMFontInit
TTFFontInit
Issue1343
LabelTTFAlignment
LabelBMFontBounds
TTFFontShadowAndStroke
LabelBMFontCrash
层景-Layer
CCTextureAtlas
菜单-Menu
MotionStreak
节点-Node
(定位点)-anchorPoint and children
(标签)-tags
(移除节点)-remove and cleanup
(移除/清理节点)-remove/cleanup with children
(压力测试)-stress test #1: no crashes
(压力测试)Stress泄漏stress test #2: no leaks
(定时器)cocosnode scheduler test #1
(精灵节点作为父节点)nodeToParent transform
(摄像机轨道)Camera Orbit test
(摄像机移动)Camera Zoom test
(摄像机中心)Camera Center test
(世界坐标转换为节点坐标)testing convertToNodeSpace / AR. Touc
(掩盖)
(混合颜色)GL_BLEND
Parallax
Particle
Performance
RenderTexture
RotateWorld
场景-Scene
Scheduler
Shader
骨骼-Spine
(骨骼动画)
精灵-Sprite
(批处理精灵)SpriteBatchNode (tap screen)
(精灵颜色+透明度)Sprite: Color & Opacity
(批处理精灵颜色+透明度)SpriteBatchNode: Color & Opacity
(精灵z轴显示顺序)Sprite: Z order
(批处理精灵z显示顺序)SpriteBatchNode: Z order
(重新排序)SpriteBatchNode: reorder
(定点)Sprite: openGL Z vertex
(批处理精灵)SpriteBatchNode: openGL Z vertex
(锚点)Sprite: anchor point
(批处理精灵锚点)SpriteBatchNode: anchor point
(批处理精灵-变形)SpriteBatchNode transformation
(精灵-翻转)Sprite Flip X & Y
(批处理精灵-翻转)SpriteBatchNode Flip X & Y
(锯齿)Sprite Aliased
(批处理精灵-锯齿)SpriteBatchNode Aliased
(精灵新纹理)
(批处理精灵切换纹理)SpriteBatchNode new texture (tap)
(精灵框架)SpriteFrames are obtained using the alias na
(精灵锚点偏移)
(精灵批处理锚点旋转)SpriteBatchNode offset + anchor + rot
(精灵设置锚点缩放)Sprite offset + anchor + scale
(批处理精灵锚点设置缩放)SpriteBatchNode offset + anchor + sca
(精灵动画)Sprite: Animation + flip
(精灵混合)HybrCCSprite* sprite Test
(组合精灵)SpriteBatchNode Grand Children
(精灵显示顺序)SpriteBatchNode Children Z
(精灵的可见性显示)Sprite & SpriteBatchNode Visibility
(批处理精灵与锚点)SpriteBatchNode: children + anchor
(批处理精灵缩放)Sprite/BatchNode + child + scale + rot
(多层子类精灵)Sprite multiple levels of children
(精灵批处理)Sprite + children + skew
(没有纹理的精灵)Sprite without texture
(创建精灵)Testing initWithTexture:rect method
(两种显示效果对比) Retina Display. SD (left) should be equ
(动画缓存)Sprite should be animated
(精灵动画缓存-加载资源)AnimationCache - Load file
(精灵偏移+锚点+缩放)Sprite offset + anchor + scale
(批处理精灵偏移+锚点+缩放) SpriteBatchNode offset + anchor +
(精灵偏移+锚点+翻转)Sprite offset + anchor + flip
(批处理精灵偏移+锚点+翻转)SpriteBatchNode offset + anchor + f
(节点排序)node sort same index
SpriteBatchNode + children + rot skew
Sprite rot skew + negative scale with children
SpriteBatchNode anchor + rot skew + scale
Sprite anchor + rot skew + scale
SpriteBatchNode offset + anchor + rot skew
Sprite offset + anchor + rotational skew
SpriteBatchNode reorder 1 child
TextInput
Texture2d
TextureCache
TexturePackerEncryption
TileMap
Touches
(球碰撞测试)
Transitions
UserDefault
(小型数据保存XML 读写测试)
Zwoptex-创建纹理工具
项目实战
一个都不能死项目
思路:
资源文件合成与拆分
实现步骤
NO Zuo NO Die
萝莉快跑
象棋
打飞机
参考
颜色对照表
环境搭建 Cocos2dx 搭建 1.cocos2dx 2.2.3 windows 开发环境搭建过程 win7 64 位 + vs2013 + cocos2dx 2.2.3 + python2.7 1) 安装 python2.7,安装过程默认即可 2)设置环境变量 Path,加上 c:\python27 3)解压 cocos2dx 2.2.3zip 文件到你想要的目录,比如 d:\cocos2d-x-2.2.3, 注意中间目录不要带中文,空格 4)开打 cmd,cd 到 d:\cocos2d-x-2.2.3\tools\project-creator, 执行 create_project.py 创建项目 create_project.py -project 项目名字 -package android 包名 -language cpp 包名必须是含有两个”.”之上的域名,比如 org.wlin.cocos2dx 如果是 org.wlin 则报错。 5)打开 d:\cocos2d-x-2.2.3\projects\项目名字\proj.win32\项目名 字.sln 文件打开项目,并编译 android 开发环境搭建 1)安装 jdk 2)解压 adt-bundle 3)执行 adt-bundle/sdk/tools/android.bat 来创建模拟器 (不是必须的,如果有手机,尽 量用 android 手机) 4)执行 adt-bundle/eclipse/eclipse.exe 来创建 android 工程
android cocos 开发环境搭建 1) 解压 ndk 到 d 盘 2) 安装 cygwin64,解决 bash 找不到的问题 3) 设置 cygwin64 的 path,在 eclipse->Window->preferences->c/c++->build->Environment 增加环境变量 PATH 值 c:\cygwin64\bin 4) 增加环境变量 NDK_ROOT 值 d:\android-ndk-r9d (ndk 所在的目录) 打开 cocos2dx 工程来编译 1)在 eclipse 中,打开 cocos2dx 工程 file->new->other->android project from existing code 在 Root Directory 中,浏览到你要打开的工程目录下的 proj.android 2) 在 eclipse 中,增加 libcocos2dx 的工程(这个工程只要加一次,代码是公用的) cocos2d-x-2.2.3/cocos2dx/platform/android/java 3)修改 build_native.sh 脚本 在执行 ndk-build 之前,增加脚本: chmod 777 "$APP_ANDROID_ROOT"/assets –R 4)修改 makefile,把自己写的 cpp 文件加入到 makefile 中 makefile 路径:jni/android.mk 5)打包完成后,在工程目录/proj.android/bin/xxx.apk 文件是 android 的程序文件 Cocos2D-x 基础 什么是框架 什么是框架,框架最低要求 理论: 1)框架定义了一些协议(功能的抽象);目的:解耦合,通过函数指针实现。 2)框架实现了这层协议的调用,框架具有集成功能。 3)上层应用按照要求,去实现框架协议
代码兑现: 1)CCApplicationProtocol 类定义了一套接口 2)CCApplication 实现了接口规则的调用 3)AppDelegate 实现了接口 4)在 CCApplication::run()利用多态特性,实现父类代码去调用子类(AppDelegate)重载函 数;这也是多态的重要意义。典型的工程开发项目设计思路 AppDelegate 继承了 CCApplication class AppDelegate : private cocos2d::CCApplication CCApplication 继承了 CCApplicationProtocol class CC_DLL CCApplication : public CCApplicationProtocol Hello world 程序详解 Win32 main 函数讲解: #include "main.h" #include "AppDelegate.h" #include "CCEGLView.h" // #define USING_NS_CC using namespace cocos2d 使用cocos2d空间 USING_NS_CC; int APIENTRY _tWinMain(HINSTANCE hInstance,//本程序的句柄
HINSTANCE hPrevInstance, //前一个程序的句柄 LPTSTR int lpCmdLine, //命令行参数 nCmdShow) //界面形式参数 { // UNREFERENCED_PARAMETER是一个宏,作用是防止编译器出现函数参数未被使用 的warnning UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // create the application instance //创建应用程序的实例,其中包含了让程序存在的死循环。死循环在run()中实现。帧 循环 AppDelegate app; //然后是定义了一个AppDelegate对象app,该类继承自CCApplication,CCApplication 类的protected域定义了个CCApplication类型的指针,在CCApplication的构造函数里会赋 值为this,然后静态成员函数sharedApplication返回该指针指向对象的引用,有点像使用了 单例模式(或许其实就是单例)。 //eglView是窗口,是一个单列,通过 sharedOpenGLView来获取。 CCEGLView* eglView = CCEGLView::sharedOpenGLView(); eglView->setViewName("junit1--");//创建窗口的名字 eglView->setFrameSize(480, 320);//创建窗口的大小 return CCApplication::sharedApplication()->run();//执行帧循环, //sharedApplication 返回sm_pSharedApplication 类型的值,和app的地址值一样。 } Run()函数的帧循环 int CCApplication::run() { PVRFrameEnableControlWindow(false);//将窗口信息写入注册表之类 // Main message loop: MSG msg; LARGE_INTEGER nFreq; LARGE_INTEGER nLast; LARGE_INTEGER nNow; //高精度时间计数 QueryPerformanceFrequency(&nFreq);//获得时钟频率 QueryPerformanceCounter(&nLast);//获取当前时钟精 // Initialize instance and cocos2d. //cocos2d-x程序公共代码的真正入口 ,applicationDidFinishLaunching 是个全虚函 数,子类实现,父类调用
if (!applicationDidFinishLaunching()) { return 0; } CCEGLView* pMainWnd = CCEGLView::sharedOpenGLView(); pMainWnd->centerWindow(); ShowWindow(pMainWnd->getHWnd(), SW_SHOW); while (1) { if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { // Get current time tick. QueryPerformanceCounter(&nNow); // If it's the time to draw next frame, draw it, else sleep a while. if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart) //m_nAnimationInterval保存了帧频率,可以通过CCDirector单例对象修 改 { nLast.QuadPart = nNow.QuadPart; //进入真正的消息循环的处理函数是 CCDirector::sharedDirector()->mainLoop(); CCDirector::sharedDirector()->mainLoop(); /* mainLoop是纯虚函数,这里由子类CCDisplayLinkDirector实现,并且为 程序创建了其全局变量变量。 重点是drawScene(),在mainLoop中调用,这个函数里重绘所有的图形 对象object。 这里要写一点,cocos2d里的所有object都是保存在一棵树结构里进行 维护。重绘时,调用object的成员函数vist进行树的遍历。 每个结点都保存了zOrder的值(我们addObject时的第二个参数),每个结 点的所有子结点都根据这个zOrder值进行排序,于是,遍历的顺序就改变了,重绘的次 序也就改变了,这就是cocos2d处理层的方法。条理清晰,井井有序 */ } else { Sleep(0);
} continue; } if (WM_QUIT == msg.message) { // Quit message loop. break; } // Deal with windows message. if (! m_hAccelTable || ! TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } applicationDidFinishLaunching 虚函数的讲解 //cocos2d-x程序公共代码的真正入口 bool AppDelegate::applicationDidFinishLaunching() { // initialize director //创建导演 CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); pDirector->setOpenGLView(pEGLView); // turn on display FPS //设置是否打开调试信息显示 pDirector->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this //设置帧频 pDirector->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object //创建场景 ,自动释放 CCScene *pScene = HelloWorld::scene(); // run
//运行场景 pDirector->runWithScene(pScene); return true; } Hello::scene 场景的初始化 CCScene* HelloWorld::scene() { // 'scene' is an autorelease object //创建场景对象,自动管理内存 CCScene *scene = CCScene::create(); // 'layer' is an autorelease object //创建层,create 调用了init() 初始化。 HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene //将层加入到场景中 scene->addChild(layer); // return the scene return scene; } HelloWorld::init() 初始化 // on "init" you need to initialize your instance //被layer对象调用。 bool HelloWorld::init() { ////////////////////////////// // 1. super init first //init 不是构造器,调用子类的init并不会自动调用父类的init,所以要显式调用父类 的init() if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();//获取画框的大小 CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();//获取画框中的左下 角坐标点 ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it.
//增加一个推出按钮 // add a "close" icon to exit the progress. it's an autorelease object CCMenuItemImage *pCloseItem = CCMenuItemImage::create( 标 "CloseNormal.png",//初始显示的图标 "CloseSelected.png",//被按下后显示的图 this,//按钮被触发后调用的对象 menu_selector(HelloWorld::menuCloseCallback) //按钮被触发后调用对象中的函数。 ); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2));// 设置位置 // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); //创建菜单项 pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1);//将菜单加入节点 ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label //创建标签 CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen //标签设置坐标 pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); // add the label as a child to this layer //将标签加入到层的节点 this->addChild(pLabel, 1); // add "HelloWorld" splash screen" //创建精灵对象,使用图片初始化 CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // position the sprite on the center of the screen
分享到:
收藏