3.0版本
比如就在HelloCPP里面动态创建一个TEXT并设置一些属性
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
auto 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(HelloWorld::menuCloseCallback,this));
closeItem->setPosition(origin + Point(visibleSize) - Point(closeItem->getContentSize() / 2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
/*
auto label = LabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE);
// position the label on the center of the screen
label->setPosition(Point(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height));
// add the label as a child to this layer
this->addChild(label, 1);*/
Text* label_name = Text::create();
label_name->setColor(Color3B::WHITE);
label_name->setFontSize(25);
label_name->setPosition(Point(origin.x + visibleSize.width / 2,
origin.y + visibleSize.height/2));
label_name->setSize(Size(visibleSize.width, visibleSize.height));
label_name->setText("abcdefg");
label_name->setTextHorizontalAlignment(TextHAlignment::LEFT);
this->addChild(label_name);
return true;
}
你会发现 abcdefg 这几个字处于屏幕正中央,而不是左对齐!
求解!是BUG还是我的用法不对
