一个cocos2dx 游戏解读(AppDelegate)

来源:互联网 发布:手机代驾软件 编辑:程序博客网 时间:2024/06/03 13:49

原文c#版本

http://www.cnblogs.com/nowpaper/archive/2012/09/11/2680852.html

另外一个改写的c++版本

http://www.cnblogs.com/tetris/archive/2012/12/17/2821616.html

我直接在cocos2dx SimpleGame demo下进行的

文件目录如下


assets里面放资源文件,hellocpp这个是cocos2dx的demo源码


Android.mk 中修改

LOCAL_SRC_FILES :=hellocpp/main.cpp \                  AppDelegate.cpp \                  hellocpp\HelloWorldScene.cpp \                  hellocpp\GameOverScene.cpp \                   GameRoot.cpp \                   Scenes/SceneGame.cpp \                   Scenes/SceneOver.cpp \                   Scenes/SceneSelect.cpp \                   Scenes/SceneSetting.cpp \                   Scenes/SceneStart.cpp \                   UI/LayerLevels.cpp \                   Roles/Actor.pp \                   Roles/ActorBase.cpp \                   Roles/ActorData.cpp

我这里把demo源码也编译了,AppDelegate.cpp 如果替换成hellocpp/AppDelegate.cpp,那么这个程序启动的就是cocos2dx的demo

下面是我对这个的解读


bool AppDelegate::applicationDidFinishLaunching(){//初始化CCDirector    // initialize director    CCDirector *pDirector = CCDirector::sharedDirector();    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();    CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize();    CCSize designSize = CCSizeMake(800, 480);    float resScale = designSize.width/designSize.height;    float screenScale = screenSize.width/screenSize.height;    pEGLView->setDesignResolutionSize(screenSize.width, screenSize.height, kResolutionShowAll);    /**     * http://blog.csdn.net/go_to_learn/article/details/8302699     */    if (screenSize.height > 480)    {    pDirector->setContentScaleFactor(designSize.width/screenSize.width);}else    {pDirector->setContentScaleFactor(screenSize.height/designSize.height);    }    // enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices.//     pDirector->enableRetinaDisplay(true);    // turn on display FPS    //是否显示FPS(每秒帧速率)    pDirector->setDisplayStats(false);    // set FPS. the default value is 1.0/60 if you don't call this    // 在这里设置Updata的间隔    pDirector->setAnimationInterval(1.0 / 60);    GameRoot::InitializeResource();    // create a scene. it's an autorelease object    //CCScene *pScene = HelloWorld::scene();    // 创建一个场景    CCScene *pScene = new SceneStart();    // run    // 运行这个场景    pDirector->runWithScene(pScene);    return true;}

有一个地方原文是没有的,这主要是为了缩放 代码如下

 CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize(); CCSize designSize = CCSizeMake(800, 480); float resScale = designSize.width/designSize.height; float screenScale = screenSize.width/screenSize.height; pEGLView->setDesignResolutionSize(screenSize.width, screenSize.height, kResolutionShowAll);
screenSize 获取屏幕大小designSize 资源图片大小,他这个资源是800*480的resScale 计算资源图片宽高比screenScale 屏幕的宽高比setDesignResolutionSize 这个是设置缩放后的适应策略http://blog.csdn.net/go_to_learn/article/details/8302699 讲的很清楚
    if (screenSize.height > 480)    {    pDirector->setContentScaleFactor(designSize.width/screenSize.width);    }else {pDirector->setContentScaleFactor(screenSize.height/designSize.height);    }

setContentScaleFactor设置缩放

这个地方就用到了宽高比,首先判断是需要缩小还是放大,因为android 屏幕类型太多这里就说一种情况 1280*720手机

因为图片没有手机屏幕打所以需要放大,但是这个宽高比例和图片比例是不一样的,如果按照高度缩放,那么图片缩放以后只有1200*720,会少80;

如果要铺满屏幕就需要用宽度来缩放,但是用宽度就会得到的高度大于720,这个时候就需要上面一个方法了

如果要写的完美需要多套图片,还要根据主流机型进行缩放,这个只是写一种情况。

 GameRoot::InitializeResource();  

这一句是初始化资源,本例中是吧小图拼接成一个大图,具体可以看原帖


AppDelegate主要是初始化项目,熟悉ios'的很清楚

在这个里面加载了CCScene场景

这里说一下cocos2dx的主要结构 

这个来源http://www.cnblogs.com/lhming/archive/2012/07/01/2572217.html


导演(CCDiretor) 

     在cocos2d-x引擎中,CCDirector类是整个游戏的组织和控制核心,游戏的运行规则,游戏内的CCScene(场景)、布景(CCLayer)、角色(CCSprite)等的运动,均由CCDirector管理,其在游戏中起着指定游戏规则让游戏内的场景、布景和任务有序的运行

摄像机(CCCamera)  
    游戏中每个节点都需要摄像机,当节点发生缩放,旋转和位置变化时,都需要覆盖摄像机,让这个节点通过摄像机进行重新渲染。

场景(CCScene)  
    在游戏里,场景就是关卡。关卡由人物角色和背景构成。

布景(CCLayer) 
    从概念上说,布景就是场景里的背景。

角色(CCSprite)  
精灵是整个游戏开发处理的主要对象,包括主角和敌人、NPC等,甚至随机飘过的一片云或飞鸟从技术上讲,也是精灵,因为精灵在cocos2d-x中,就是一个可以不断变化的图片,这些变化包括位置变化,旋转、放大缩小和运动等。 

动作(CCAction) 
    角色所具有的动作,一般用于精灵发生动作时使用,如移动,释放魔法等。