cocos2d-x ios游戏开发初认识(一)

对于cocos2d引擎的安装上章已经经过,为了与视频学习保存一致,装的是2.1.4的版本。

用xcode新建工程,我用版本是xcode5开始运行会出现两个错误,首先进行改错。

错误代码:

clang: error: unknown argument: '-websockets' [-Wunused-command-line-argument-hard-error-in-future]

只需将other link中-websockets删掉就行


然后运行就ok了。

运行结果:


看函数的主代码部分:

和做ios开发一样从AppDelegate 启动函数开始看:

boolAppDelegate::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();


    // run

    pDirector->runWithScene(pScene);


    return true;

}

先画张图从大的方面分析



其中 CCDirector是导演类 sharedDirector函数

CCDirector*CCDirector::sharedDirector(void)

{

    if (!s_SharedDirector)

    {

        s_SharedDirector =new CCDisplayLinkDirector();

        s_SharedDirector->init();

    }


    returns_SharedDirector;

}

作用用来生成一个单例。 

在往下看  pDirector->setDisplayStats(true); 使用表示 是否显示FPS(每秒中填充图像的帧数)的一些信息 就是左下角显示的那些信息

pDirector->setAnimationInterval(1.0 / 60);  表示每秒60帧


接着看很重要的类 CCScene 场景类

classCC_DLL CCScene : publicCCNode  继承至CCNode

{

public:

    CCScene();

   virtual ~CCScene();

   bool init();


   static CCScene *create(void);

};

这是最基本的 第一个为构造函数 第二个为析构函数 第三个为初始化函数 第四个为类函数

下面注重分析  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);


    // preprocessor macro for "static create()" constructor ( node() deprecated )

    CREATE_FUNC(HelloWorld);

};

virtual bool init(); 是个虚函数 在c++中主要用来实现多态 具体看它的实现方法

bool HelloWorld::init()

{

    //

    // 1. super init first

   if ( !CCLayer::init() )  //先初始化父类的init方法

    {

        return false;

    }

/*

这里就像ios开发中初始化一样

if(self = [super init])

{

}

*/

/*

下面的后面都是用到和解释


*/

    /

    // 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 -20, 20) );


    // create menu, it's an autorelease object

   CCMenu* pMenu = CCMenu::create(pCloseItem,NULL);

    pMenu->setPosition(CCPointZero );

   this->addChild(pMenu,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);


    // 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;

}

接下来看 CCScene 类方法

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;                             //返回这个场景

}


CCScene *pScene = HelloWorld::scene(); //就是创建一个HelloWorld的场景


CREATE_FUNC(HelloWorld); //很重要 打开这个宏

#define CREATE_FUNC(__TYPE__) \  //__TYPE__ 就是 HelloWorld

static __TYPE__* create() \    //相当于 static HelloWorld*create() HelloWorld的类方法 作用就是返回HelloWorld对象

{ \

    __TYPE__ *pRet = new __TYPE__(); \  //HelloWorld *pRet = new HelloWorld();

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

    { \

        pRet->autorelease(); \          //加入自动释放池

        return pRet; \                  //返回HelloWorld对象

    } \

    else \

    { \

        delete pRet; \

        pRet = NULL; \

        return NULL; \

    } \

}



pDirector->runWithScene(pScene);    //让导演去运行这个场景





















  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值