博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Cocos2dx3.2从零开始【二】继续Cocos2dx3.2
阅读量:2430 次
发布时间:2019-05-10

本文共 3382 字,大约阅读时间需要 11 分钟。

继续Cocos2dx3.2的学习,遇到问题并记录。

一、设置竖屏

     1.Cocos2dx 3.2中   

//glview = GLView::create("My Game");//原来 glview = GLView::createWithRect("My Game", Rect(0.0f, 0.0f, 480.0f, 640.0f));//现在,改变尺寸,暂先用这个。
    2.移植到Android中

     在AndroidManifest.xml文件中,修改landscape为portrait。

android:screenOrientation="portrait"

二、 4种创建精灵的方法

    //====创建精灵的四种方法    CCSprite * spr1 = CCSprite::create("Icon.png");    spr1->setPosition(ccp(70, 150));    this->addChild(spr1);        //参数  图片名称   矩形区域    CCSprite * spr2 = CCSprite::create("Icon.png",CCRectMake(0, 0, 30, 30));    spr2->setPosition(ccp(150, 150));    this->addChild(spr2);    //利用帧缓存中的一帧的名称声称一个对象    //参数  帧的名称        CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("test_icon.plist");    CCSprite * spr3 = CCSprite::createWithSpriteFrameName("Icon.png");    spr3->setPosition(ccp(230, 150));    this->addChild(spr3);    //通过另一帧生成    //利用另外一帧生成一个精灵对象    //参数   精灵对象        CCSpriteFrame * frame = CCSpriteFrame::create("Icon.png", CCRectMake(0, 0, 40, 30));    CCSprite * spr4 = CCSprite::createWithSpriteFrame(frame);    spr4->setPosition(ccp(310, 150));    addChild(spr4);    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("test_icon.plist");

三、Cocos2dx中添加类需要添加到Class文件夹。添加后,如果打包到Android,需要到proj.android/jni/Android.mk文件中添加。

四、去掉左下角信息。

AppDelegate里注释掉turn on display FPS。或设置为false。

五、中文乱码。

暂用转换格式函数。

//转换编码格式std::string FSAToU8(const std::string& a_sString){	static std::wstring_convert
> c_cvt_u8; static std::wstring_convert
> c_cvt_a(new std::codecvt
("")); return c_cvt_u8.to_bytes(c_cvt_a.from_bytes(a_sString));}std::string FSWToU8(const std::wstring& a_sString){ static std::wstring_convert
> c_cvt_u8; return c_cvt_u8.to_bytes(a_sString);}
注意: 此函数必须写在init函数之前,C/C++之类的都有顺序问题 。多谢孙哥提供函数。
调用直接 
auto label = LabelTTF::create(FSAToU8("测试"), "Arial", 24);

再修改!

移植到Eclipse报错!经多次调试,最终解决方案如下:

修改上述方法为:

#if CC_TARGET_PLATFORM != CC_PLATFORM_WIN32#define FSAToU8(str) std::string(str)#else//转换编码格式std::string FSAToU8(const std::string& a_sString){	static std::wstring_convert
> c_cvt_u8; static std::wstring_convert
> c_cvt_a(new std::codecvt
("")); return c_cvt_u8.to_bytes(c_cvt_a.from_bytes(a_sString));}std::string FSWToU8(const std::wstring& a_sString){ static std::wstring_convert
> c_cvt_u8; return c_cvt_u8.to_bytes(a_sString);}#endif
调用不变

auto label = LabelTTF::create(FSAToU8("测试"), "Arial", 24);

错误记录

#define FSAToU8(str) new std::string(str)由于没有delete造成内存泄漏,将new去掉!

六、命令行打包apk文件

打开命令行,定位到项目根目录,输入

cocos compile -p android –-ap 20
回车。

注意:如果Android\sdk\platforms目录下没有android-20,需要一个空的。

七、添加文字的三种办法

std::string words = "测试";   auto label = Label::create(words,"STHeiti", 30);   this->addChild(label);     TTFConfigconfig("fonts/myFont1.ttf", 25);//TTF字体   auto labelTTF =Label::createWithTTF(config, "测试");   labelTTF->setPosition(333,333);   labelTTF->setColor(Color3B(255, 0, 0));   this->addChild(labelTTF);     auto labelTTF1 =Label::createWithTTF("测试", "fonts/myFont2.ttf", 25);   labelTTF1->setTextColor(Color4B(255, 0,0, 255));   labelTTF1->setPosition(444, 444);   labelTTF1->enableShadow(Color4B::BLUE,Size(10,-10));   labelTTF1->enableOutline(Color4B::GREEN,3);   labelTTF1->enableGlow(Color4B::BLACK);   this->addChild(labelTTF1);

后:怎么自动添加了这么多!

转载地址:http://pvnmb.baihongyu.com/

你可能感兴趣的文章
回首互联网十年,我们能从八次烧钱大战中学到什么
查看>>
漫画:如何辨别二逼互联网公司!?
查看>>
麒麟信安面向场景化创新,赋能openEuler商业验证
查看>>
王者又连跪了?快让 AI 帮你上分!
查看>>
1 分钟带你认识从 "�" 到 "锟斤拷"
查看>>
3 年培养 10 万“码农”,郑州推出“码农计划”
查看>>
一个三本程序猿的大厂逆袭之路
查看>>
程序员弃码投中医?还做成了不错的生意! | 极客视频
查看>>
百度一 29 岁程序员因“篡改数据”被抓
查看>>
去年我年薪 30W,今年我一天做 3 顿饭
查看>>
入职大厂,我容易吗?
查看>>
《互联网人退化简史》
查看>>
CTO 写的低级 Bug 再致网站被黑,CEO 的号都被盗了!
查看>>
955 加班少的公司名单来了!
查看>>
狂赚 1227 亿!腾讯员工 2020 年人均年薪 81 万;小米员工人均年薪 45 万
查看>>
漫画:什么是加密算法?
查看>>
程序员有话说 |当那个不靠谱的程序员跟我做同一个项目时
查看>>
怎样以程序员的方式来用百度呢?
查看>>
程序员是如何运用增长思维找到女朋友?
查看>>
@程序员,离职让企业损失近900亿,还遭疯抢!他凭什么?
查看>>