Cocos2d-x源代码剖析-SimpleGame代码剖析



龙舞奉天
2014-03-16 01:16:13

[p=26, null, left][color=#333333][backcolor=rgb(255, 255, 255)][font=Arial][size=14px]今天偶然看见一篇关于剖析cocos2dx基本机理的文章,觉得很是不错,虽说是作者去年写的,但对一些cocos2d想新手来说,还是很不错的,大家一起来欣赏一下。[/size][/font][/backcolor][/color][/p][p=26, null, left][color=#333333][backcolor=rgb(255, 255, 255)][font=Arial][size=14px]转自:[url]http://blog.csdn.net/jacklam200/article/details/8963724[/url][/size][/font][/backcolor][/color][/p]
[p=26, null, left][color=#333333][backcolor=rgb(255, 255, 255)][font=Arial][size=14px]拿到了Andreas Loew TexturePacker和PhysicsEditor的key 和keyfile,可以开始Cocos2d-x源代码剖析了,感谢Cocos2d-x能开源,感谢Andreas Loew能免费给开发者使用他的TexturePacker和PhysicsEditor[/size][/font][/backcolor][/color][/p][p=26, null, left][color=#333333][backcolor=rgb(255, 255, 255)][font=Arial][size=14px]
[/size][/font][/backcolor][/color][/p][p=26, null, left][color=#333333][backcolor=rgb(255, 255, 255)][/backcolor][/color][/p][color=#333333][backcolor=rgb(255, 255, 255)][font=Arial][size=14px][b] Cocos2d-x[font=黑体]源代码剖析(SimpleGame代码剖析)[/font][/b]1.SimpleGame[font=宋体]目录结构剖析[/font]
首先先从SimpleGame[font=宋体]目录结构入手,着重以[/font]Android[font=宋体]平台为例![/font]Cocos2dX Android SimpleGame[font=宋体]主要涉及到了[/font]java jni[font=宋体],[/font]android ndk[font=宋体]知识,所以对这方面必须有所了解。[/font]
[img]http://img.blog.csdn.net/20130523102026494[/img]


SimpleGame[font=宋体]目录如上所示,[/font]classes[font=宋体]放着基于[/font]Cocos2dx[font=宋体]的游戏逻辑实现文件,实现的语言为[/font]c++

[img]http://img.blog.csdn.net/20130523102029825[/img]Proj.android [font=宋体]则为[/font]cocos2d -x android[font=宋体]程序的呈现,里面主要有[/font]android[font=宋体]的[/font]jni[font=宋体]和[/font]android [font=宋体]的[/font]apk[font=宋体]代码结构,其中[/font]buid_nativie.sh[font=宋体]主要是把[/font]Resources[font=宋体]下的资源往[/font]android[font=宋体]的[/font]asset[font=宋体]文件夹复制搬移,且如果有[/font]icon[font=宋体]图则把[/font]icon[font=宋体]图复制到[/font]drawable[font=宋体]下,然后用[/font]ndk[font=宋体]编译[/font]JNI,[font=宋体]形成[/font]libgame[font=宋体]。[/font]So[font=宋体]文件[/font]
[img]http://img.blog.csdn.net/20130523102032885[/img]
Resources[font=宋体]文件夹则放着游戏的资源[/font]
[img]http://img.blog.csdn.net/20130523102035638[/img]

2.SimpleGame[font=宋体]代码剖析[/font]
[font=宋体]
[/font]

首先从Android[font=宋体]的文件结构([/font]proj.Android[font=宋体])开始[/font]
当我们打开eclipse[font=宋体]的时候,[/font]project.properties文件会告诉我们需要使用什么样的ANDROID SDK[font=宋体],需要哪些第三方库,本工程是否作为库文件形式及混淆等等。我们来看看[/font]simpleGame[font=宋体]的是怎样的[/font]
[font=Consolas,][color=silver][backcolor=rgb(248, 248, 248)][font=Verdana, Geneva, Arial, Helvetica, sans-serif][size=9px][b][html][/b] [color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]view plain[/url][/size][/backcolor][/color][color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]copy[/url][/size][/backcolor][/color]
[/size][/font][/backcolor][/color]

[list=1]
[*][color=black][backcolor=inherit]# Project target. [/backcolor][/color]
[*][color=black][backcolor=inherit]target=android-8 [/backcolor][/color]
[*][color=black][backcolor=inherit]android.library.reference.1=../../../../cocos2dx/platform/android/java [/backcolor][/color]
[/list][/font]

如图所示,android sdk[font=宋体]目标版本[/font]ANDROID2.2,[font=宋体]而本工程依赖于cocos2d-2.1rc0-x-2.1.3\cocos2dx\platform\android\java[/font]
的工程,也就是说导入simplegame[font=宋体]的同时,也要导入这个工程,[/font]而这个工程主要是实现java[font=宋体]通过[/font]jni[font=宋体]调用加载游戏逻辑,并且让[/font]jni[font=宋体]下的[/font]C[font=宋体]程序能调用[/font]java[font=宋体]层对[/font]ANDROID[font=宋体]系统的一些[/font]API[font=宋体]调用,下一章将会对这部分进行分析,现在先略过[/font]
接着我们查看Android [font=宋体]的[/font]AndroidManifest.xml


[font=Consolas,][color=silver][backcolor=rgb(248, 248, 248)][font=Verdana, Geneva, Arial, Helvetica, sans-serif][size=9px][b][html][/b] [color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]view plain[/url][/size][/backcolor][/color][color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]copy[/url][/size][/backcolor][/color]
[/size][/font][/backcolor][/color]

[list=1]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit][*][color=black][backcolor=inherit] package="org.cocos2dx.simplegame" [/backcolor][/color]
[*][color=black][backcolor=inherit] android:versionCode="1" [/backcolor][/color]
[*][color=black][backcolor=inherit] android:versionName="1.0"> [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] [*][color=black][backcolor=inherit] android:icon="@drawable/icon"> [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] [*][color=black][backcolor=inherit] android:label="@string/app_name" [/backcolor][/color]
[*][color=black][backcolor=inherit] android:screenOrientation="landscape" [/backcolor][/color]
[*][color=black][backcolor=inherit] android:theme="@android:style/Theme.NoTitleBar.Fullscreen" [/backcolor][/color]
[*][color=black][backcolor=inherit] android:configChanges="orientation"> [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit]
[/backcolor][/color]
[*][color=black][backcolor=inherit]
[/backcolor][/color]
[*][color=black][backcolor=inherit]
[/backcolor][/color]
[*][color=black][backcolor=inherit] [*][color=black][backcolor=inherit] android:smallScreens="true" [/backcolor][/color]
[*][color=black][backcolor=inherit] android:anyDensity="true" [/backcolor][/color]
[*][color=black][backcolor=inherit] android:normalScreens="true"/> [/backcolor][/color]
[*][color=black][backcolor=inherit]
[/backcolor][/color]
[/list][/font]


从里面可以看出,需要gles2.0[font=宋体]的支持,[/font]
程序的主入口activity[font=宋体]为[/font]org.cocos2dx.simplegame.SimpleGame

我们顺着这条线继续走,定位到org.cocos2dx.simplegame[font=宋体]的[/font]SimpleGame.java
[font=Consolas,][color=silver][backcolor=rgb(248, 248, 248)][font=Verdana, Geneva, Arial, Helvetica, sans-serif][size=9px][b][java][/b] [color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]view plain[/url][/size][/backcolor][/color][color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]copy[/url][/size][/backcolor][/color]
[/size][/font][/backcolor][/color]

[list=1]
[*][color=black][backcolor=inherit]package org.cocos2dx.simplegame; [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit]import org.cocos2dx.lib.Cocos2dxActivity; [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit]import android.os.Bundle; [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit]public class SimpleGame extends Cocos2dxActivity{ [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] protected void onCreate(Bundle savedInstanceState){ [/backcolor][/color]
[*][color=black][backcolor=inherit] super.onCreate(savedInstanceState); [/backcolor][/color]
[*][color=black][backcolor=inherit] } [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] static { [/backcolor][/color]
[*][color=black][backcolor=inherit] System.loadLibrary("game"); [/backcolor][/color]
[*][color=black][backcolor=inherit] } [/backcolor][/color]
[*][color=black][backcolor=inherit]} [/backcolor][/color]
[/list][/font]

从代码可以看出,此Activity[font=宋体]继承了cocos2d-2.1rc0-x-2.1.3\cocos2dx\platform\android\java[/font]
工程下Cocos2dxActivty,[font=宋体]并加载了用[/font]native_build.sh[font=宋体]编译[/font]jni[font=宋体]出来的[/font]libgame.so
我们先简单介绍下Cocos2dxActivty[font=宋体](后续会在[/font]cocos2dx java[font=宋体]引用库剖析),[/font]Cocos2dxActivty[font=宋体]主要做了有如下的工作:[/font]
初始化ndk[font=宋体]层需要的实例[/font]
设置布局,把GLSurfaceView和Render[font=宋体]和[/font]edittext[font=宋体]设置好[/font],[font=宋体]加载入程序界面[/font]


好了来看看重点,cocos2dx[font=宋体]是用[/font]C++[font=宋体]实现的,所以游戏逻辑也用[/font]c++[font=宋体]来实现,[/font]ANDROID[font=宋体]主要就是通过[/font]jni[font=宋体]形式,来操作[/font]android[font=宋体]程序界面[/font]
我们先来看看jni[font=宋体]下的目录结构[/font]
[font=宋体][img]http://img.blog.csdn.net/20130523150120802[/img]
[/font]

主要来看Android.mk,android.mk[font=宋体]说明了此[/font]jni[font=宋体]编译成共享库为[/font]libgame.so[font=宋体],所需源文件为[/font]hellocpp下的main[font=宋体]。[/font]Cpp[font=宋体]和[/font]classes[font=宋体]文件夹下的[/font]cpp[font=宋体],并使用了[/font]cocos2dx[font=宋体]的[/font]cocos2dx_static cocosdenshion_static cocos_extension_static三个静态库
其他不理先,我们先看我们看得到的main.cpp[font=宋体]和[/font]classes[font=宋体]文件夹下的[/font]cpp
[font=Consolas,][color=silver][backcolor=rgb(248, 248, 248)][font=Verdana, Geneva, Arial, Helvetica, sans-serif][size=9px][b][cpp][/b] [color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]view plain[/url][/size][/backcolor][/color][color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]copy[/url][/size][/backcolor][/color]
[/size][/font][/backcolor][/color]

[list=1]
[*][color=black][backcolor=inherit]void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h) [/backcolor][/color]
[*][color=black][backcolor=inherit]{ [/backcolor][/color]
[*][color=black][backcolor=inherit] if (!CCDirector::sharedDirector()->getOpenGLView()) [/backcolor][/color]
[*][color=black][backcolor=inherit] { [/backcolor][/color]
[*][color=black][backcolor=inherit] CCEGLView *view = CCEGLView::sharedOpenGLView(); [/backcolor][/color]
[*][color=black][backcolor=inherit] view->setFrameSize(w, h); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] AppDelegate *pAppDelegate = new AppDelegate(); [/backcolor][/color]
[*][color=black][backcolor=inherit] CCApplication::sharedApplication()->run(); [/backcolor][/color]
[*][color=black][backcolor=inherit] } [/backcolor][/color]
[*][color=black][backcolor=inherit] /* [/backcolor][/color]
[*][color=black][backcolor=inherit] else [/backcolor][/color]
[*][color=black][backcolor=inherit] { [/backcolor][/color]
[*][color=black][backcolor=inherit] ccDrawInit(); [/backcolor][/color]
[*][color=black][backcolor=inherit] ccGLInvalidateStateCache(); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] CCShaderCache::sharedShaderCache()->reloadDefaultShaders(); [/backcolor][/color]
[*][color=black][backcolor=inherit] CCTextureCache::reloadAllTextures(); [/backcolor][/color]
[*][color=black][backcolor=inherit] CCNotificationCenter::sharedNotificationCenter()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); [/backcolor][/color]
[*][color=black][backcolor=inherit] CCDirector::sharedDirector()->setGLDefaultValues(); [/backcolor][/color]
[*][color=black][backcolor=inherit] } [/backcolor][/color]
[*][color=black][backcolor=inherit] */ [/backcolor][/color]
[*][color=black][backcolor=inherit]} [/backcolor][/color]
[/list][/font]



图为main.cpp[font=宋体]。熟悉[/font]cocos2dx[font=宋体]一看都知道这是函数的主入口,此函数主要给[/font]org.cocos2dx.lib.Cocos2dxRenderer[font=宋体]类使用,而此类为上面所提到的[/font]Cocos2dxActivity[font=宋体]初始化的[/font]GLSurfaceview[font=宋体]的[/font]Render[font=宋体],[/font]Render[font=宋体]会在其可见的时候初始化此函数(也就是说[/font]android SimpleGame Activity[font=宋体]开启的时候就调用了此函数)。[/font]

我们顺着这条线继续剖析下去!
[img]http://img.blog.csdn.net/20130523150301763[/img]

上图截取自Cocos2dx[font=宋体]游戏开发一书的图,我们可以大概知道[/font]Cocos2dx[font=宋体]是设计游戏是一个怎样的模式[/font]
[img]http://img.blog.csdn.net/20130523150330388[/img]

同样上图则是cocos2dx[font=宋体]的实现模式[/font]
[font=Consolas,][color=silver][backcolor=rgb(248, 248, 248)][font=Verdana, Geneva, Arial, Helvetica, sans-serif][size=9px][b][cpp][/b] [color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]view plain[/url][/size][/backcolor][/color][color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]copy[/url][/size][/backcolor][/color]
[/size][/font][/backcolor][/color]

[list=1]
[*][color=black][backcolor=inherit]void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h) [/backcolor][/color]
[*][color=black][backcolor=inherit]{ [/backcolor][/color]
[*][color=black][backcolor=inherit] if (!CCDirector::sharedDirector()->getOpenGLView()) [/backcolor][/color]
[*][color=black][backcolor=inherit] { [/backcolor][/color]
[*][color=black][backcolor=inherit] CCEGLView *view = CCEGLView::sharedOpenGLView(); [/backcolor][/color]
[*][color=black][backcolor=inherit] view->setFrameSize(w, h); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] AppDelegate *pAppDelegate = new AppDelegate(); [/backcolor][/color]
[*][color=black][backcolor=inherit] CCApplication::sharedApplication()->run(); [/backcolor][/color]
[*][color=black][backcolor=inherit] } [/backcolor][/color]
[*][color=black][backcolor=inherit]} [/backcolor][/color]
[/list][/font]

再回来看看源码,从图可以看出,先实例化了一个Director[font=宋体](一场戏最主要要有一个导演来主持),再[/font]openglview[font=宋体]为空的情况下,创建了[/font]openGLVIEW[font=宋体],并设置他的高宽,然后新建一个[/font]AppDelegate app[font=宋体]实例[/font],[font=宋体]并运行此实例[/font].

App[font=宋体]的运行带动[/font]CCApplication[font=宋体]调用[/font]applicationDidFinishLaunching,所以在AppDelegate[font=宋体]内的[/font]applicationDidFinishLaunching首先被调用
[font=Consolas,][color=silver][backcolor=rgb(248, 248, 248)][font=Verdana, Geneva, Arial, Helvetica, sans-serif][size=9px][b][cpp][/b] [color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]view plain[/url][/size][/backcolor][/color][color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]copy[/url][/size][/backcolor][/color]
[/size][/font][/backcolor][/color]

[list=1]
[*][color=black][backcolor=inherit]bool AppDelegate::applicationDidFinishLaunching() { [/backcolor][/color]
[*][color=black][backcolor=inherit] // initialize director [/backcolor][/color]
[*][color=black][backcolor=inherit] CCDirector *pDirector = CCDirector::sharedDirector(); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] pDirector->setOpenGLView(CCEGLView::sharedOpenGLView()); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize(); [/backcolor][/color]
[*][color=black][backcolor=inherit] CCSize designSize = CCSizeMake(480, 320); [/backcolor][/color]
[*][color=black][backcolor=inherit] std::vector searchPaths; [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] if (screenSize.height > 320) [/backcolor][/color]
[*][color=black][backcolor=inherit] { [/backcolor][/color]
[*][color=black][backcolor=inherit] searchPaths.push_back("hd"); [/backcolor][/color]
[*][color=black][backcolor=inherit] searchPaths.push_back("sd"); [/backcolor][/color]
[*][color=black][backcolor=inherit] pDirector->setContentScaleFactor(640.0f/designSize.height); [/backcolor][/color]
[*][color=black][backcolor=inherit] } [/backcolor][/color]
[*][color=black][backcolor=inherit] else [/backcolor][/color]
[*][color=black][backcolor=inherit] { [/backcolor][/color]
[*][color=black][backcolor=inherit] searchPaths.push_back("sd"); [/backcolor][/color]
[*][color=black][backcolor=inherit] pDirector->setContentScaleFactor(320.0f/designSize.height); [/backcolor][/color]
[*][color=black][backcolor=inherit] } [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionNoBorder); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] // turn on display FPS [/backcolor][/color]
[*][color=black][backcolor=inherit] pDirector->setDisplayStats(true); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] // set FPS. the default value is 1.0/60 if you don't call this [/backcolor][/color]
[*][color=black][backcolor=inherit] pDirector->setAnimationInterval(1.0 / 60); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] // create a scene. it's an autorelease object [/backcolor][/color]
[*][color=black][backcolor=inherit] CCScene *pScene = HelloWorld::scene(); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] // run [/backcolor][/color]
[*][color=black][backcolor=inherit] pDirector->runWithScene(pScene); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] return true; [/backcolor][/color]
[*][color=black][backcolor=inherit]} [/backcolor][/color]
[/list][/font]

程序的流程如下
[img]http://img.blog.csdn.net/20130523150534684[/img]
[font=宋体]
[/font]
从代码看到其场景为HelloWorld::scene(),我们进入这个函数看看,
[font=Consolas,][color=silver][backcolor=rgb(248, 248, 248)][font=Verdana, Geneva, Arial, Helvetica, sans-serif][size=9px][b][cpp][/b] [color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]view plain[/url][/size][/backcolor][/color][color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]copy[/url][/size][/backcolor][/color]
[/size][/font][/backcolor][/color]

[list=1]
[*][color=black][backcolor=inherit]CCScene* HelloWorld::scene() [/backcolor][/color]
[*][color=black][backcolor=inherit]{ [/backcolor][/color]
[*][color=black][backcolor=inherit] CCScene * scene = NULL; [/backcolor][/color]
[*][color=black][backcolor=inherit] do [/backcolor][/color]
[*][color=black][backcolor=inherit] { [/backcolor][/color]
[*][color=black][backcolor=inherit] // 'scene' is an autorelease object [/backcolor][/color]
[*][color=black][backcolor=inherit] scene = CCScene::create(); [/backcolor][/color]
[*][color=black][backcolor=inherit] CC_BREAK_IF(! scene); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] // 'layer' is an autorelease object [/backcolor][/color]
[*][color=black][backcolor=inherit] HelloWorld *layer = HelloWorld::create(); [/backcolor][/color]
[*][color=black][backcolor=inherit] CC_BREAK_IF(! layer); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] // add layer as a child to scene [/backcolor][/color]
[*][color=black][backcolor=inherit] scene->addChild(layer); [/backcolor][/color]
[*][color=black][backcolor=inherit] } while (0); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] // return the scene [/backcolor][/color]
[*][color=black][backcolor=inherit] return scene; [/backcolor][/color]
[*][color=black][backcolor=inherit]} [/backcolor][/color]
[/list][/font]

从代码可以看出此函数创建了一个scene[font=宋体],并把创建好,并[/font]init[font=宋体]([/font]create[font=宋体]为宏定义,里面会调用到[/font]init[font=宋体])好的[/font]HelloWorld layer[font=宋体]加载入场景中[/font]

接着我们来看看Helloworld layer[font=宋体]做了什么事[/font]
首先Helloworld[font=宋体]继承[/font]CLayerColor[font=宋体],而[/font]CLayerColor[font=宋体]继承[/font]CCLayerRGBA接口为CCBlendProtocol,CCLayerRGBA继承CCLayer接口为CCRGBAProtocol,可见Helloworld[font=宋体]是一个[/font]Layer
因为在创建场景的时候,也创建了Helloworld layer[font=宋体]([/font]create[font=宋体]函数),而[/font]create[font=宋体]函数会调用[/font]init[font=宋体]这个函数,所以[/font]helloworld layer[font=宋体]的初始化都在[/font]init[font=宋体]这个函数内[/font]
[font=Consolas,][color=silver][backcolor=rgb(248, 248, 248)][font=Verdana, Geneva, Arial, Helvetica, sans-serif][size=9px][b][cpp][/b] [color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]view plain[/url][/size][/backcolor][/color][color=#a0a0a0][backcolor=inherit][size=9px][url=http://blog.csdn.net/jacklam200/article/details/8963724#]copy[/url][/size][/backcolor][/color]
[/size][/font][/backcolor][/color]

[list=1]
[*][color=black][backcolor=inherit]bool HelloWorld::init() [/backcolor][/color]
[*][color=black][backcolor=inherit]{ [/backcolor][/color]
[*][color=black][backcolor=inherit] bool bRet = false; [/backcolor][/color]
[*][color=black][backcolor=inherit] do [/backcolor][/color]
[*][color=black][backcolor=inherit] { [/backcolor][/color]
[*][color=black][backcolor=inherit] ////////////////////////////////////////////////////////////////////////// [/backcolor][/color]
[*][color=black][backcolor=inherit] // super init first [/backcolor][/color]
[*][color=black][backcolor=inherit] ////////////////////////////////////////////////////////////////////////// [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] CC_BREAK_IF(! CCLayerColor::initWithColor( ccc4(255,255,255,255) ) ); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] ////////////////////////////////////////////////////////////////////////// [/backcolor][/color]
[*][color=black][backcolor=inherit] // add your codes below... [/backcolor][/color]
[*][color=black][backcolor=inherit] ////////////////////////////////////////////////////////////////////////// [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] // 1. Add a menu item with "X" image, which is clicked to quit the program. [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] // Create a "close" menu item with close icon, it's an auto release object. [/backcolor][/color]
[*][color=black][backcolor=inherit] CCMenuItemImage *pCloseItem = CCMenuItemImage::create( [/backcolor][/color]
[*][color=black][backcolor=inherit] "CloseNormal.png", [/backcolor][/color]
[*][color=black][backcolor=inherit] "CloseSelected.png", [/backcolor][/color]
[*][color=black][backcolor=inherit] this, [/backcolor][/color]
[*][color=black][backcolor=inherit] menu_selector(HelloWorld::menuCloseCallback)); [/backcolor][/color]
[*][color=black][backcolor=inherit] CC_BREAK_IF(! pCloseItem); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] // Place the menu item bottom-right conner. [/backcolor][/color]
[*][color=black][backcolor=inherit] CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); [/backcolor][/color]
[*][color=black][backcolor=inherit] CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2, [/backcolor][/color]
[*][color=black][backcolor=inherit] origin.y + pCloseItem->getContentSize().height/2)); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] // Create a menu with the "close" menu item, it's an auto release object. [/backcolor][/color]
[*][color=black][backcolor=inherit] CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); [/backcolor][/color]
[*][color=black][backcolor=inherit] pMenu->setPosition(CCPointZero); [/backcolor][/color]
[*][color=black][backcolor=inherit] CC_BREAK_IF(! pMenu); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] // Add the menu to HelloWorld layer as a child layer. [/backcolor][/color]
[*][color=black][backcolor=inherit] this->addChild(pMenu, 1); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] ///////////////////////////// [/backcolor][/color]
[*][color=black][backcolor=inherit] // 2. add your codes below... [/backcolor][/color]
[*][color=black][backcolor=inherit] CCSprite *player = CCSprite::create("Player.png", CCRectMake(0, 0, 27, 40) ); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] player->setPosition( ccp(origin.x + player->getContentSize().width/2, [/backcolor][/color]
[*][color=black][backcolor=inherit] origin.y + visibleSize.height/2) ); [/backcolor][/color]
[*][color=black][backcolor=inherit] this->addChild(player); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] this->schedule( schedule_selector(HelloWorld::gameLogic), 1.0 ); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] this->setTouchEnabled(true); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] _targets = new CCArray; [/backcolor][/color]
[*][color=black][backcolor=inherit] _projectiles = new CCArray; [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] // use updateGame instead of update, otherwise it will conflit with SelectorProtocol::update [/backcolor][/color]
[*][color=black][backcolor=inherit] // see [url]http://www.cocos2d-x.org/boards/6/topics/1478[/url] [/backcolor][/color]
[*][color=black][backcolor=inherit] this->schedule( schedule_selector(HelloWorld::updateGame) ); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background-music-aac.wav", true); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] bRet = true; [/backcolor][/color]
[*][color=black][backcolor=inherit] } while (0); [/backcolor][/color]
[*][color=black][backcolor=inherit] [/backcolor][/color]
[*][color=black][backcolor=inherit] return bRet; [/backcolor][/color]
[*][color=black][backcolor=inherit]} [/backcolor][/color]
[/list][/font]

首先,创建了一个MenuItem[font=宋体](加载了[/font]CloseNormal[font=宋体]和[/font]CloseSelected[font=宋体]两张图片),回调为[/font]menucloseCallback[font=宋体],并加载到[/font]CCMenu[font=宋体]中,作为当点击的时候关闭程序,而[/font]CCMenu[font=宋体]则添加到[/font]Helloworld[font=宋体]层中,并设置位置[/font]
接着新建player[font=宋体]精灵,设置其位置,并加入[/font]Helloworld layer[font=宋体]中。并开启一个[/font]timer[font=宋体]调用[/font]gamelogic[font=宋体]这个函数,[/font]gamelogic[font=宋体]主要创建一些[/font]Target[font=宋体]精灵,并给他们设定动画[/font],[font=宋体]跑动动画,结束时的动画[/font]
再接着,开启另一个Timer[font=宋体]用于游戏的刷新([/font]updateGame[font=宋体]),刷新了子弹精灵,[/font]target[font=宋体]精灵,并把被打中的进行删除和打出的子弹进行删除,如果打中大于等于[/font]5.[font=宋体]就更换到[/font]GAMEOVERSCENE
子弹的发射主要在ccTouchesEnded[font=宋体]的时候触发,把发出的子弹加入[/font]Helloworld layer[font=宋体]中,并设置了子弹的动画[/font]

在GameOverScene[font=宋体]中[/font]init[font=宋体]只是加了一层显示结果的[/font]label[font=宋体],到一定时间就转回[/font]HelloWorld [font=宋体]的[/font]layer


SimpleGame[font=宋体]就解析到此,文采不好,此记录只作为自己记忆所用[/font]
[/size][/font][/backcolor][/color]


lrb2010
2014-03-18 17:40:41

支持斑竹君...有时间可以修改一下排版


龙舞奉天
2014-03-18 19:22:18

[quote][size=2][color=#999999]lrb2010 发表于 2014-3-18 17:40[/color] [url=forum.php?mod=redirect&goto=findpost&pid=342962&ptid=26196][img]static/image/common/back.gif[/img][/url][/size]
支持斑竹君...有时间可以修改一下排版[/quote]

OK,等我把剩余的教程写完了发上来后,就来改这些排版{:21:}