Cocos2dx3.2从零开始【二】继续Cocos2dx3.2

本文详细记录了使用Cocos2dx3.2进行游戏开发时,如何设置竖屏显示,以及介绍了4种创建精灵的方法,并解决了中文乱码问题和命令行打包apk文件的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

继续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种创建精灵的方法

<del>    //====创建精灵的四种方法
    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");</del>

三、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<std::codecvt_utf8<wchar_t>> c_cvt_u8;
	static std::wstring_convert<std::codecvt<wchar_t, char, mbstate_t>> c_cvt_a(new std::codecvt<wchar_t, char, mbstate_t>(""));
	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<std::codecvt_utf8<wchar_t>> 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<std::codecvt_utf8<wchar_t>> c_cvt_u8;
	static std::wstring_convert<std::codecvt<wchar_t, char, mbstate_t>> c_cvt_a(new std::codecvt<wchar_t, char, mbstate_t>(""));
	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<std::codecvt_utf8<wchar_t>> 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); 

后:怎么自动添加了这么多!
<span style="font-size:18px;"></span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值