cocos2d-x 基础回想

     开始学习cocos2d-x,做个记录给以后的自己翻看。有点点的C++编程基础而已。悲剧~~~~~~~学习了一个星期。

   (1)  创建一个类:1.#pragma once

                             2.引入头文件:#include "cocos2d.h",USING_NS_CC,using namespace cocos2d(在头文件中有第二个就不需要了重复了);这三个不要忘记。

                                 .h文件中   CREATE_FUNC(myScene);  重写create方法不然以后出错适用create创建时候。

                              3.C++的方法在.cpp中时候 void myClass::myFunction();

    (2)创建一个场景: 1.  static CCScene* scene();    virtual bool init();
                                             2.类要继承于CClayer   --> ::public CClayer  

                                            3.将层加到场景中,初始化init层

                                                CCScene* myScene::scene()
                                                {
                                                          CCScene* scene01 = CCScene::create();
                                                          myScene* sceneLayer = myScene::create();
                                                          scene01->addChild(sceneLayer);

                                                          menuLayer* layer01 =menuLayer::create();
                                                          layer01->setVisible(false);
                                                          scene01->addChild(layer01,1,2);

                                                          return scene01;
 
                                                     }


                                                   bool myScene::init()
                                                    {
                                                             if (!CCLayer::init())
                                                              {
                                                                        return false;
                                                              }

                                                             retrun true;

                                                     }

           (3)设置屏幕大小:AppDelegate.cpp

                                                  CCEGLView* myView = CCEGLView::sharedOpenGLView();
                                                  myView->setFrameSize(960,640);
                                                   pDirector->setOpenGLView(myView);

            (4)场景切换:CCDirector::sharedDirector()->replaceScene(需要转换的场景);

                                       场景特效:CCDirector::sharedDirector()->replaceScene(CCTransitionFade::transitionWithDuration(时间,场景,颜色));

           (5)精灵控件:  CCSprite* mysprite = CCSprite::create("");
                                           mysprite->setPosition(ccp());
                                           this->addChild(mysprite);                                           

           (6)组合精灵控件:CCRenderTexture --->"CompositeFigureSprite.h  .....cpp"  

             (7) 菜单:CCMenuItemSprite* myItem = CCMenuItemSprite::initWithNormalSprite(nomalSprite,selectedSprite,this,menu_selector());

                                //men_selector()的使用要注意回调函数带有个CCobject * 参数,代表着个CCMenuItemSprite.

                               myItem->setPosition(ccp());
                               CCMenu* rMenu = CCMenu::createWithItem(myItem);
                               rMenu->addChild(myItem2);
                               rMenu->setPosition(ccp(0,0));
                               this->addChild(rMenu);
            (8)动作: CCActionInterval* moves = CCMoveTo::create(2,ccp());
                              mysprite->runAction();

                              //看下用到那个看那个

         (9)触摸:

                             virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
                             virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
                             virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
                             virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);

                              virtual void registerWithTouchDispatcher(void);//注册

                             .cpp

                                this->setTouchEnabled(true);        

 

                               void menuLayer::registerWithTouchDispatcher(void){

                                             CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);

                                   }

                                bool menuLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
                                {
                                                 CCPoint touchLocation = pTouch->getLocationInView();
                                                 touchLocation = CCDirector::sharedDirector()->convertToUI(touchLocation);
                                                 touchLocation = this->convertToNodeSpace(touchLocation);
                                                 touchLocation = ccp(touchLocation.x , 640 - touchLocation.y); //pc机器上

                                                 return true;
                                  }
                                 void menuLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){};
                                void menuLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){};
                                 void menuLayer::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent){};
     

      (10)openGL坐标转换:

                     // get Point 图片的x坐标,y坐标, 图片宽,图片高
                   CCPoint menuLayer::getPoint(float x, float y , float width , float heigh)
                     {
                                   float cenX =x+ width /2 ;
                                   float cenY = 640 -y-heigh/2;
                                  CCPoint point =  ccp(cenX,cenY);
                                  return  point;
                  }

     (11)注意:使用CCMenuItemSprite想改变它的sprite时候,要在回调函数中新create一个,之前的已经自动释放。

      (12) 得到场景中的层: menuLayer* mylayer = (  menuLayer* )CCDirector::sharedDirector()->getRunningScene()->getChildByTag(2);                      

      (13)进入场景调用的函数:   //场景淡入的几个事件
                                                            virtual   void onEnter();
                                                            virtual void onEnterTransitionDidFinish();
                                                            virtual void onExit();

          (14)熟悉基本算法。

 

 

 

 

你可能感兴趣的:(cocos2d-x)