cocos2d第一天总结

只是列出部分代码:

main这个函数是程序的入口:

int APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
    AppDelegate app;                 //AppDelegate是CCApplication的代理,有了这个对象之后就会自动调用父类CCApplication里面的构造方法

//(CCApplication *CCApplication::sm_pSharedApplication = 0;  通过  sm_pSharedApplication = this;将app赋值给pSharedApplication,详细可以参考CCApplication类)


    CCEGLView* eglView = CCEGLView::sharedOpenGLView();
    eglView->setViewName("TestGame");              //标题
    eglView->setFrameSize(480, 320);                   //窗体的大小
    return CCApplication::sharedApplication()->run();     //这句话有几层意思,CCApplication::sharedApplication()这句话会返回一个sm_pSharedApplication,这个对象,通过这个对象调用run()方法,run方法里面有个判断语句  if (!applicationDidFinishLaunching())  {return 0; }},applicationDidFinishLaunching()这个才是真正游戏的开始,AppDelegate.cpp里面的,也就是applicationDidFinishLaunching()       方法

AppDelegate.cpp:首先看看这个代理里面的的方法:

// initialize director(初始化导演类)
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
    //设置试图
    pDirector->setOpenGLView(pEGLView);
    
    // turn on display FPS(打开fps)
    pDirector->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this(设置针数)
    pDirector->setAnimationInterval(1.0 / 60);
(创建场景,会自动销毁)
    CCScene *pScene = day_01cclayercolor::ccSene();      //这句话很重要,创建场景,调用我们自己建立的类里面的方法
    // run(游戏真正开始)
    pDirector->runWithScene(pScene);
    return true;

day_01cclayercolor::ccSene();通过这句话调用day_01cclayercolor里面的ccSene()方法


CCScene * day_01cclayercolor::ccSene(){
    //创建场景
    CCScene *sene = CCScene::create();

  day_01cclayercolor *layer = day_01cclayercolor::create();    //创建层,调用day_01cclayercolor.h中的CREATE_FUNC(day_01cclayercolor.),进行init初始化
//上面这句话很重要,在创建layer的同时调用day_01cclayercolor.h里面的CREATE_FUNC(day_01cclayercolor);按住f12可以进入查看代码怎么执行(下面就是进入CREATE_FUNC里面的语句)

/*

#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
    __TYPE__ *pRet = new __TYPE__(); \          //__TYPE__这个就相当于day_01cclayercolor,好好理解一下,相当于接受day_01cclayercolor的参数,
    if (pRet && pRet->init()) \                  //这里调用  day_01cclayercolor.cpp里面的init方法
    { \
        pRet->autorelease(); \
        return pRet; \
    } \
    else \
    { \
        delete pRet; \
        pRet = NULL; \
        return NULL; \
    } \
}
  */
    sene->addChild(layer);
    return sene;            
}


init()方法里面就进行一些初始化,比如精灵的创建,等等

下面是我整个过程的代码:附加解释:

                                                                                                                            
                                                                                                      main.cpp

int APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // create the application instance
    AppDelegate app;//AppDelegate是CCApplication的代理
    CCEGLView* eglView = CCEGLView::sharedOpenGLView();
    eglView->setViewName("TestGame");//标题
    eglView->setFrameSize(480, 320);//窗体的大小
    return CCApplication::sharedApplication()->run();
}
                                                                                                                              day_01cclayercolor.h

#pragma once
//定义一个有颜色的layer(层)
#ifndef _day_01cclayercolor_H_
#define __day_01cclayercolor_H_
#include "cocos2d.h"
USING_NS_CC;
class day_01cclayercolor:public CCLayerColor
{
public:
    day_01cclayercolor();
    ~day_01cclayercolor();
    static CCScene *ccSene();
    bool init();
    CREATE_FUNC(day_01cclayercolor);
};
#endif
                                                                                                                         day_01cclayercolor.cpp
#include "day_01cclayercolor.h"


day_01cclayercolor::day_01cclayercolor()
{
}


day_01cclayercolor::~day_01cclayercolor()
{
}


CCScene * day_01cclayercolor::ccSene(){
    //创建场景
    CCScene *sene = CCScene::create();
    //创建层,调用day_01cclayercolor.h中的CREATE_FUNC,进行init初始化
    day_01cclayercolor *layer = day_01cclayercolor::create();
    sene->addChild(layer);
    return sene;
}
bool day_01cclayercolor::init(){
    CCLayerColor::initWithColor(ccc4(255,255,255,255),100,100);      //CCLayerColor初始化
    ignoreAnchorPointForPosition(false);
    return true;
}
                                                                                                     AppDelegate.h

#ifndef  _APP_DELEGATE_H_
#define  _APP_DELEGATE_H_

#include "cocos2d.h"
class  AppDelegate : private cocos2d::CCApplication
{
public:
    AppDelegate();
    virtual ~AppDelegate();
    virtual bool applicationDidFinishLaunching();
    virtual void applicationDidEnterBackground();
    virtual void applicationWillEnterForeground();
};

#endif // _APP_DELEGATE_H_

                                                                                                                          AppDelegate.cpp

#include "AppDelegate.h"
#include "HelloWorldScene.h"
#include "MySecene.h"
#include "day_01zorder.h"
#include  "day_01cclayercolor.h"
USING_NS_CC;

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate()
{
}
/*
简单的实现ccdirector和ccscene初始化代码在这里。
初始化成功返回TRUE,程序继续。
返回false初始化失败,应用程序终止。
*/
//程序启动完成后从main函数进入这个方法
bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director(初始化导演类)
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
    //设置试图
    pDirector->setOpenGLView(pEGLView);
    
    // turn on display FPS(打开fps)
    pDirector->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this(设置针数)
    pDirector->setAnimationInterval(1.0 / 60);
    CCScene *pScene = day_01cclayercolor::ccSene();
    // run(游戏真正开始)
    pDirector->runWithScene(pScene);
    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
//程序进入后台时进入这个方法,比如打电话之类的
void AppDelegate::applicationDidEnterBackground() {
//通过这句话将声音进行暂停
    CCDirector::sharedDirector()->stopAnimation();

    // if you use SimpleAudioEngine, it must be pause
    // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
//程序重新被激活,重新启动动画
void AppDelegate::applicationWillEnterForeground() {
    CCDirector::sharedDirector()->startAnimation();

    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}



这才cocos2d引擎的执行流程,学习cocos2d一定要搞清楚这些流程,也许我讲解的不够好,欢迎大家提意见!!相互讨论

你可能感兴趣的:(cocos2d第一天总结)