cocos2d-x学习(一) HelloWorld

前两天搭好了windows+VS2010+cocos2d-x 和MacMini+xcode+cocos2d-x两个开发环境。两个开发环境下的HelloWorld测试程序和自己添加的HelloWorld测试程序、

通过编译并顺利运行,具体的配置办法来自网络资料介绍,感谢强大的网路。万里长征第一步,在此Mark一下。

具体环境搭建方法网络上有许多的介绍,在此就不做复述。下面就自己对HelloWorld做具体分析。

因为Win32下和iOS下HelloWorld的结构十分相似,就那iOS下HelloWorld做举例分析。

首先程序启动进入程序入口函数,在main.m文件下

int main(int argc, char *argv[]) {

    

    NSAutoreleasePool * pool = [[NSAutoreleasePoolalloc] init];

    int retVal = UIApplicationMain(argc, argv, nil, @"AppController");

    [pool release];

    return retVal;

}

然后进入

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions函数。在该函数中创建窗口,并调

 cocos2d::CCApplication::sharedApplication()->run();该函数调用

bool AppDelegate::applicationDidFinishLaunching()函数

bool AppDelegate::applicationDidFinishLaunching()

{

    // initialize director

    CCDirector *pDirector = CCDirector::sharedDirector();//创建出导演类对象

    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

 

    // 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();//HelloWorld的scene方法实际上是创建了一个CCScene 对象(autorelease,关于autorelease属性,个人理解为类似于C++中智能指针,采用引用计数的方法管理内存)

     // run

    pDirector->runWithScene(pScene);

 

    returntrue;

}

下面就HelloWorld类做点分析

在HelloWorld.h文件中有个宏定义很关键

 CREATE_FUNC(HelloWorld);

它的具体代码为

#define CREATE_FUNC(__TYPE__) \

static __TYPE__* create() \

{ \

    __TYPE__ *pRet = new __TYPE__(); \

    if (pRet && pRet->init()) \

    { \

        pRet->autorelease(); \

        return pRet; \

    } \

    else \

    { \

        delete pRet; \

        pRet = NULL; \

        return NULL; \

    } \

}

实际上这个宏定义中会调用HelloWorld中的init方法。

CCScene* HelloWorld::scene()

{

    // 'scene' is an autorelease object

    CCScene *scene = CCScene::create();//关键代码,创建CCScene对象

    

    // 'layer' is an autorelease object

    HelloWorld *layer = HelloWorld::create();

 

    // add layer as a child to scene

    scene->addChild(layer);//在scene上添加layer.

 

    // return the scene

    return scene;

}

 

 

// on "init" you need to initialize your instance

boolHelloWorld::init()//layer上添加的具体精灵的实现

{

    //////////////////////////////

    // 1. super init first

    if ( !CCLayer::init() )

    {

        returnfalse;

    }

      /////////////////////////////

    // 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(CCDirector::sharedDirector()->getWinSize().width/2 , CCDirector::sharedDirector()->getWinSize().width/2) );

    /////////////////////////////

    // 3. add your codes below..

    // add a label shows "Hello World"

    // create and initialize a label

    CCLabelTTF* pLabel = CCLabelTTF::create("Box of chen", "Thonburi", 34);

    // ask director the window size

    // position the label on the center of the screen

    pLabel->setPosition( ccp(CPercent::GetPercent(size.width, 50), CPercent::GetPercent(size.height, 90)) );

    // add the label as a child to this layer

    this->addChild(pLabel, 1);

    // add "HelloWorld" splash screen"

    CCSprite* pSprite = CCSprite::create("bgimg.jpg");

    // position the sprite on the center of the screen

    pSprite->setPosition( ccp(size.width/2, size.height/2) );

     // add the sprite as a child to this layer

    this->addChild(pSprite, 0);

    returntrue;

}

 

void HelloWorld::menuCloseCallback(CCObject* pSender)

{

    CCDirector::sharedDirector()->end();

 

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    exit(0);

#endif

}

 

posted @ 2013-03-18 20:43  酱酱爱  阅读(442)  评论(0编辑  收藏  举报