继续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>