Cocos2dx之AppDelegate类

举报
yd_221104950 发表于 2020/12/03 00:12:38 2020/12/03
【摘要】 AppDelegate是一个很重要的类。它只会被调用一次。游戏就是从这个类开始的。这个类的关键部分: 1、设计分辨率 有助我们决定我们的Sprite(精灵)对象要多大等。这是基于设备的屏幕尺寸的。AppDelegate与分辨率相关的: static cocos2d::Size designResolutionSize = cocos2d::Size(480, 32...

AppDelegate是一个很重要的类。它只会被调用一次。游戏就是从这个类开始的。这个类的关键部分:

1、设计分辨率

有助我们决定我们的Sprite(精灵)对象要多大等。这是基于设备的屏幕尺寸的。AppDelegate与分辨率相关的:

static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320);
static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320);
static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768);
static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536);

  
 
  • 1
  • 2
  • 3
  • 4

2、AppDelegate::applicationDidFinishLaunching()

从这个方法开始编码我们的游戏:

	// initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) glview = GLViewImpl::createWithRect("MyGame", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
#else glview = GLViewImpl::create("MyGame");
#endif director->setOpenGLView(glview); } // turn on display FPS director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0f / 60); // Set the design resolution glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER); auto frameSize = glview->getFrameSize(); // if the frame's height is larger than the height of medium size. if (frameSize.height > mediumResolutionSize.height) { director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width)); } // if the frame's height is larger than the height of small size. else if (frameSize.height > smallResolutionSize.height) { director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width)); } // if the frame's height is smaller than the height of medium size. else { director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width)); } register_all_packages(); // create a scene. it's an autorelease object auto scene = HelloWorld::createScene(); // run director->runWithScene(scene);

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

3、Director导演

Director对象控制操作流并通知必要的接受者该做什么。Director的一个重要任务就是控制Scene(场景)的替换和过渡。Director对象是一个共享的单例。
(1)获取Director实例

// initialize director
auto director = Director::getInstance();

  
 
  • 1
  • 2

(2)展示场景

// run
director->runWithScene(scene);

  
 
  • 1
  • 2

(3)替换场景

// use when changing from the running scene to another scene
director->replaceScene(scene2); 

  
 
  • 1
  • 2

(4)暂停游戏

Director::getInstance()->stopAnimation();

  
 
  • 1

(5)开始游戏

Director::getInstance()->startAnimation();

  
 
  • 1

(4)、获取/设置游戏的属性

// turn on display FPS
director->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0f / 60);
// set content scale factor
director->setContentScaleFactor(...);


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_40763897/article/details/104330493

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。