Cocos2d-x学习--IOS(1)

来源:互联网 发布:yum lmysqlclient 编辑:程序博客网 时间:2024/06/10 20:32

1.Cocos2d-x是跨平台的游戏引擎。本文中使用的cocos2d-2.1rc0-x-2.1.3版本,使用Xcode4.6。

1.1具体的安装过程。终端进入下载文件的目录,输入 ./install-templates-xcode.sh -u

1.2Cocos2d-x有4个常用类

CCSprite、CCLayer、CCScene、CCDirector

分别是游戏中的角色(精灵),布局层,场景,导演

2.学习方式:

2.1需找其中的规律。

2.2研究引擎自带的子程序,其中的demo包容各个类能实现的效果等等

2.3主动搜索,首先推荐google

3.在创建工程的时候,会生成默认代码。

AppDelegate存在3个生命周期函数:

bool AppDelegate::applicationDidFinishLaunching(){    // initialize director    CCDirector *pDirector = CCDirector::sharedDirector();//一款游戏中仅存在一个CCDirector    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();    // 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 toovoid AppDelegate::applicationDidEnterBackground()//程序被用户切入后台运行时,会响应这个函数,可将一些数据保存等等{    CCDirector::sharedDirector()->stopAnimation();    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();    SimpleAudioEngine::sharedEngine()->pauseAllEffects();}// this function will be called when the app is active againvoid AppDelegate::applicationWillEnterForeground()//程序从后台切换到当前运行程序会响应这个函数{    CCDirector::sharedDirector()->startAnimation();    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();    SimpleAudioEngine::sharedEngine()->resumeAllEffects();}

新建工程时,生成的HelloWorld解析

class HelloWorld : public cocos2d::CCLayer{public:    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
    virtual bool init();//初始化函数    // there's no 'id' in cpp, so we recommend to return the class instance pointer    static cocos2d::CCScene* scene();//静态创建函数        // a selector callback    void menuCloseCallback(CCObject* pSender);//menu菜单的一个回调函数    // preprocessor macro for "static create()" constructor ( node() deprecated )    CREATE_FUNC(HelloWorld);//即使老版本的create()函数};

/** * define a create function for a specific type, such as CCLayer * @__TYPE__ class type to add create(), such as CCLayer */#define CREATE_FUNC(__TYPE__) \static __TYPE__* create() \{ \//创建一个type类型的对象    __TYPE__ *pRet = new __TYPE__(); \    if (pRet && pRet->init()) \    { \//让其自动释放        pRet->autorelease(); \        return pRet; \    } \    else \    { \//如果创建失败,安全删除type类型的对象        delete pRet; \        pRet = NULL; \        return NULL; \    } \}
CCScene* HelloWorld::scene(){    // 'scene' is an autorelease object//创建一个场景对象,是一个自动释放的对象    CCScene *scene = CCScene::create();        // 'layer' is an autorelease object     HelloWorld *layer = HelloWorld::create();    // add layer as a child to scene    scene->addChild(layer);//将布局层放入场景中    // return the scene    return scene;}


bool HelloWorld::init(){    //////////////////////////////    // 1. super init first  //1.先将父类初始化    if ( !CCLayer::init() )    {        return false;    }    /////////////////////////////    // 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) );//创建Menu的菜单项    pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );    // create menu, it's an autorelease object    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);//创建menu    pMenu->setPosition( CCPointZero );//设置位置    this->addChild(pMenu, 1);//将mune加入布局层,z轴为1    /////////////////////////////    // 3. add your codes below...    // add a label shows "Hello World"    // create and initialize a label    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34);    // ask director the window size    CCSize size = CCDirector::sharedDirector()->getWinSize();    // position the label on the center of the screen    pLabel->setPosition( ccp(size.width / 2, size.height - 20) );    // add the label as a child to this layer    this->addChild(pLabel, 1);//将HelloWorld的Label加入布局层,在z轴上和menu同一层面    // add "HelloWorld" splash screen"    CCSprite* pSprite = CCSprite::create("HelloWorld.png");    // 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);        return true;}void HelloWorld::menuCloseCallback(CCObject* pSender){    CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)//判断当前设备是否为ios设备    exit(0);#endif}




原创粉丝点击