Cocos2dx之AppDelegate

网友投稿 576 2022-05-30

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

Cocos2dx之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

5G游戏 Cocos2D

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Redis 企业应用场景
下一篇:Linux unzip/zip 命令用法
相关文章