zoukankan      html  css  js  c++  java
  • 基于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平台时是这个样子的:

    原因是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屏幕了。

  • 相关阅读:
    C#基础第五天
    基础学习14天 MD5加密
    C#基础第二天
    C#基础学习第一天
    Privacy Policy of ColorfulBroswer
    asp.net mvc 上传图片 摘自mvc 高级编程第311页
    多彩浏览器win10版 隐私声明
    uwp获取版本信息win10 VersionInfo
    uwp ,win10 post json
    windows phone 8.0 app 移植到windows10 app 页面类
  • 原文地址:https://www.cnblogs.com/Ray1024/p/5545449.html
Copyright © 2011-2022 走看看