LabelScene.cpp
#include "LabelScene.h"
#include "iconv/iconvString.h"//这里报错,请查看我写得另一篇文章cocos2d-x 3.4 中文乱码解决之道
USING_NS_CC;
#include "ui/CocosGUI.h"
using namespace ui;
Scene* LabelScene::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = LabelScene::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool LabelScene::init()
{
//////////////////////////////
// 1. super init first
if (!Layer::init())
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(LabelScene::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width / 2,
origin.y + closeItem->getContentSize().height / 2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
/*
auto array_plist = Array::createWithContentsOfFile("chinese.plist");
std::string info_str1;//存储新年快乐的字符串
std::string info_str2;//存储马上有钱的字符串
/ *for (int i = 0; icount(); i++)
{
auto dic = (Dictionary*)array_plist->getObjectAtIndex(i);//获取array_plist中的第一个对象
int key_id1 = 10;//因为在plist文档中,“新年快乐!”对应的id是10
int key_id2 = 20;//马上好运的id
int key_id_plist = ((String*)dic->objectForKey("id"))->intValue();//获取字典中“id”的数值,将String型的转成整型的
if (key_id1 == key_id_plist)
{
info_str1 = ((String*)dic->objectForKey("info"))->getCString();//获得”新年快乐“文字
continue;
}
if (key_id2 == key_id_plist)
{
info_str2 = ((String*)dic->objectForKey("info"))->getCString();//获得”马上有钱“文字
continue;
}
}* /
auto label_1 = LabelTTF::create(info_str1, "Arial", 25);
label_1->setPosition(Point(100, 100));
this->addChild(label_1, 1);
auto label_2 = LabelTTF::create(info_str2, "Arial", 25);
label_2->setPosition(Point(100, 200));
this->addChild(label_2, 1); */
Text* text_title = Text::create();
Text* text_sentence_one = Text::create();
Text* text_sentence_two = Text::create();
Size textsize;
textsize.width = visibleSize.width * 0.67;
textsize.height = 20;
text_title->setSize(textsize);
text_sentence_one->setSize(textsize);
text_sentence_two->setSize(textsize);
text_title->setPosition(ccp(visibleSize.width / 5, visibleSize.height*0.90f));
text_sentence_one->setPosition(ccp(visibleSize.width /5, visibleSize.height*0.90f - 200.0f));
text_sentence_two->setPosition(ccp(visibleSize.width / 5, visibleSize.height*0.90f - 400.0f));
std::vector<std::string> vecNotice;
vecNotice.push_back("喜晴(范成大)");
vecNotice.push_back("窗间梅熟落蒂,墙下笋成出林。");
vecNotice.push_back("连雨不知春去,一晴方觉夏深。");
code_convert_init();
std::string strgbk_title = gbktoutf8(vecNotice[0].c_str());
std::string strgbk_sentence_one = gbktoutf8(vecNotice[1].c_str());
std::string strgbk_sentence_two = gbktoutf8(vecNotice[2].c_str());
text_title->setText(strgbk_title);
text_sentence_one->setText(strgbk_sentence_one);
text_sentence_two->setText(strgbk_sentence_two);
text_title->setVisible(true);
text_title->setColor(Color3B::WHITE);
text_title->setFontSize(25);
addChild(text_title);
text_sentence_one->setVisible(true);
text_sentence_one->setColor(Color3B::WHITE);
text_sentence_one->setFontSize(25);
addChild(text_sentence_one);
text_sentence_two->setVisible(true);
text_sentence_two->setColor(Color3B::WHITE);
text_sentence_two->setFontSize(25);
addChild(text_sentence_two);
//创建TTF标签对象
/*
*Func 创建TTF标签对象
*@parameter1 显示文字内容
*@parameter2 字体名称
*@parameter3 字体大小
*/
auto label1 = LabelTTF::create("Create LabelTTF 1", "Marker Felt", 32);
label1->setPosition(visibleSize.width / 2+200, visibleSize.height / 2 + 200);
this->addChild(label1);
//创建TTF标签对象
/*
*Func 创建TTF标签对象
*@parameter1 显示文字内容
*@parameter2 字体名称
*@parameter3 字体大小
*@parameter4 标签大小
*@parameter5 文字水平对其方式
*/
auto label2 = LabelTTF::create("Create LabelTTF 2", "AppleGothic", 32, Size(visibleSize.width / 3, 200), TextHAlignment::LEFT);
label2->setPosition(visibleSize.width / 2+200, visibleSize.height / 2+50);
this->addChild(label2);
//创建TTF标签对象
/*
*Func 创建TTF标签对象
*@parameter1 显示文字内容
*@parameter2 字体名称
*@parameter3 字体大小
*@parameter4 标签大小
*@parameter5 文字水平对其方式
*@parameter5 文字垂直对其方式
*/
auto label3 = LabelTTF::create("Create LabelTTF 3", "Courier", 32, Size(visibleSize.width / 3, 200), TextHAlignment::LEFT, TextVAlignment::CENTER);
label3->setPosition(visibleSize.width / 2 + 200, visibleSize.height / 2);
this->addChild(label3);
//LabelBMFont and LabelAtlas
//新增标签类Label
/*
*Func 创建Label标签对象
*@parameter1 显示文字内容
*@parameter2 字体名称
*@parameter3 字体大小
*/
auto label4 = Label::create("Create Label", "Marker Felt",32);
label4->setPosition(visibleSize.width / 2 + 200, visibleSize.height / 2 - 100);
this->addChild(label4);
/*
*CC_CALLBACK_0
*CC_CALLBACK_1 表示调用的时候传递一个参数
*/
return true;
}
void LabelScene::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.", "Alert");
return;
#endif
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
LabelScene.h
#ifndef __LABELSCENE_SCENE_H__
#define __LABELSCENE_SCENE_H__
#include "cocos2d.h"
class LabelScene : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(LabelScene);
};
#endif // __LABELSCENE_SCENE_H__