基于Cocos2d-x-1.0.1的飞机大战游戏迁移到Cocos2d-x-3.0版本,并移植到Android平台成功运行

一、版本迁移中的问题

1.游戏元素Sprite、Label、Action等等的创建函数名都改为create。

2.函数的回调

callfunc_selector

callfuncN_selector

callfuncND_selector

callfuncO_selector

menu_selector

改为使用C++11的新特性std::bind和std::function配合使用:

CC_CALLBACK_0

CC_CALLBACK_1

CC_CALLBACK_2

CC_CALLBACK_3

MenuItemLabel *pauseItem = MenuItemLabel::create(
            Label::create("pause", "Adobe Ming Std L", 40),
                        this,
menu_selector(MyLayer::menuPauseCallback));

MenuItemLabel *pauseItem = MenuItemLabel::create(
            Label::create("pause", "Adobe Ming Std L", 40),
            CC_CALLBACK_1(MyLayer::menuPauseCallback,this));

3.内部类名都去掉了前缀CC,使用 clone 替代 copy,单例类采用了 getInstance 和 destroyInstance,使用了 Ref 代替了 Object,这是因为cocos2dx官方的去OC化行为

4.模板容器

使用 cocos2d::Map<> 替代了 CCDictionary ;

使用 cocos2d::Vector<> 替代了 CCArray;

5.LabelTTF / LabelBMFont / LabelAtlas三种标签类被合并成一个类Label:

1 //Label
2 Label* Label1 = Label::createWithSystemFont("1","YouYuan",30);
3 Label* Label2 = Label::createWithTTF("1","YouYuan",30);
4 Label* Label3 = Label::createWithBMFont("font/font.fnt","0123456789");

6.消息的处理方式都改为触发器模式:

1         // 键盘消息可用
2         auto listenerKey = EventListenerKeyboard::create();
3         listenerKey->onKeyPressed = CC_CALLBACK_2(PlaneWarGame::onKeyPressed, this);
4         listenerKey->onKeyReleased = CC_CALLBACK_2(PlaneWarGame::onKeyReleased, this);
5         _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerKey, this);

7.将所有标记为废弃的API替换为新的API,消除项目编译过程中的警告。

二、移植到Android平台

最初移植到Android平台时是这个样子的:

基于Cocos2d-x-1.0.1的飞机大战游戏迁移到Cocos2d-x-3.0版本,并移植到Android平台成功运行

原因是Android手机分辨率太大了,修改AppDelegate::applicationDidFinishLaunching() 函数即可:

1 static cocos2d::Size largeSize = cocos2d::Size(1080,1920);
 2 static cocos2d::Size designResolutionSize = cocos2d::Size(540, 960);
 3 
 4 bool AppDelegate::applicationDidFinishLaunching() {
 5     // initialize director
 6     auto director = Director::getInstance();
 7     auto glview = director->getOpenGLView();
 8     if(!glview) {
 9         glview = GLView::create("planeGame");
10         // mi3手机分辨率:1920x1080
11         glview->setFrameSize(largeSize.width,largeSize.height);
12         director->setOpenGLView(glview);
13         glview->setDesignResolutionSize(540, 960, ResolutionPolicy::NO_BORDER);
14     }
15     
16     director->setContentScaleFactor(0.5f);
17 
18     // turn on display FPS
19     director->setDisplayStats(true);
20 
21     // set FPS. the default value is 1.0/60 if you don't call this
22     director->setAnimationInterval(1.0 / 60);
23 
24     // create a scene. it's an autorelease object
25     auto scene = PlaneWarMenu::scene();
26 
27     // run
28     director->runWithScene(scene);
29 
30     return true;
31 }

改完之后,游戏就可以更好的适配Android屏幕了。

基于Cocos2d-x-1.0.1的飞机大战游戏迁移到Cocos2d-x-3.0版本,并移植到Android平台成功运行

原文链接: https://www.cnblogs.com/Ray1024/p/5545449.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/234300

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午4:12
下一篇 2023年2月13日 下午4:13

相关推荐