zoukankan      html  css  js  c++  java
  • 4.cocos场景和层的调用

    调用关系:

    • AppDeligate.cpp
       1 bool AppDelegate::applicationDidFinishLaunching() {
       2     // initialize director
       3     auto director = Director::getInstance();
       4     auto glview = director->getOpenGLView();
       5     if(!glview) {
       6         glview = GLViewImpl::createWithRect("MyLayer", Rect(0, 0, 960, 640));
       7         director->setOpenGLView(glview);
       8     }
       9 
      10     director->getOpenGLView()->setDesignResolutionSize(960, 640, ResolutionPolicy::SHOW_ALL);
      11 
      12     // turn on display FPS
      13     director->setDisplayStats(true);
      14 
      15     // set FPS. the default value is 1.0/60 if you don't call this
      16     director->setAnimationInterval(1.0 / 60);
      17 
      18     FileUtils::getInstance()->addSearchPath("res");
      19 
      20     // create a scene. it's an autorelease object
      21     //auto scene = HelloWorld::createScene();
      22 
      23     CCScene *pScene = MyScene::create();
      24 
      25     // run
      26     director->runWithScene(pScene);
      27 
      28     return true;
      29 }
    • MyScene.h
       1 #include "cocos2d.h"
       2 USING_NS_CC;
       3 
       4 class MyScene:public CCScene
       5 {
       6 public:
       7     static MyScene *create();
       8     bool init();
       9     
      10     
      11 };
    • MyScene.cpp
       1 #include "MyScene.h"
       2 #include "MyLayer.h"
       3 
       4 MyScene *MyScene::create()
       5 {
       6     MyScene *pRet = new MyScene;
       7     if (pRet && pRet->init())
       8     {
       9         pRet->autorelease();
      10     }
      11     else
      12     {
      13         delete pRet;
      14         pRet = NULL;
      15     }
      16     return pRet;
      17  }
      18 bool MyScene::init()
      19 {
      20     CCScene::init();
      21     MyLayer *layer = MyLayer::create();
      22     this->addChild(layer);
      23     return true;
      24 }
    • MyLayer.h
      #include "cocos2d.h"
      USING_NS_CC;
      
      class MyLayer : public CCLayer
      {
      public:
      	static MyLayer *create();
      	bool init();
      };
      

        

    • MyLayer.cpp
      #include "MyLayer.h"
      
      
      
      MyLayer *MyLayer::create()
      {
      	MyLayer *pRet = new MyLayer();
      	if (pRet && pRet->init())
      	{
      		pRet->autorelease();
      	}
      	else
      	{
      		delete pRet;
      		pRet = NULL;
      	}
      	return pRet;
       }
      
       bool MyLayer::init()
       {
      	 if (!CCLayer::init())
      	 {
      		 return false;
      	 }
      	 CCSize winSize = CCDirector::sharedDirector()->getWinSize();
      	 CCSprite *spr = CCSprite::create("shop.jpg");
      	 spr->setPosition(ccp(winSize.width / 4, winSize.height / 2));
      	 addChild(spr); 
      	 return true;
      }
      

        

  • 相关阅读:
    指针型函数与函数型指针 -2021.08.04
    Ubuntu18.04 NAT模式下配置静态IP地址 -2020.11.09
    Linux编译内核 Ubuntu18.04 -2020.11.04
    以PING为例,利用Wireshark深入理解网络层、数据链路层的工作原理 -2020.10.30
    Ubuntu18.04虚拟机的安装
    UNIX/Linux系统中的文件属性
    【计算机四级嵌入式】内存管理
    利用预编译解决C/C++重复定义的错误 -2020.09.13
    使用镜像安装cygwin、gcc并配置CLion IDE -2020.09.12
    Android Studio 4.0.1 找不到R.java 2020.09.08
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8342325.html
Copyright © 2011-2022 走看看