cocos学习笔记(四)文本
游戏中除了图像动画,最重要的传递信息的媒介就是文字。除了类似地狱边境那类游戏,大部分都是有文本交互的。 首先设定一个使用文本的目标:建立一个对话框,可以显示输入的文字(游戏中功能是显示人物对话),且这个对话框可以随时弹出、消除。 class cwind{
private:
std::string text;
static cwind *PTR;
CCSprite*wind;
std::vector<CCLabelTTF*> pLabels;
int id;
bool txton;
bool haveparent;
float txtspeed = 1.0;
cwind(){
txton = false;
haveparent = false;
wind = CCSprite::create("wind.png");
//图为960*240 实际操作范围:宽900 高170 分为4行可显示 每行225*42
wind->setAnchorPoint(ccp(0,0));
wind->setPosition(ccp(0,0));
id = 9291;
wind->setTag(id);
}
public:
static cwind*getInstance(){
if(!PTR){
PTR = new cwind();
}
return PTR;
}
void addtext(std::string str){
auto p = CCLabelTTF::create(str,"Arial",34);
p->setHorizontalAlignment(kCCTextAlignmentLeft);//左对齐
p->setAnchorPoint(ccp(0,0));//锚点 左下角
int line = pLabels.size();
p->setPosition(30,170 - line * 40);
pLabels.push_back(p);
}
void playtext(){
if (txton)return;
txton = true;
//auto *to = CCProgressTo::create(txtspeed,100);
for (auto t : pLabels){
wind->addChild(t);
//t->runAction(to);
}
}
void hidetext(){
if(txton = true)
for (auto t : pLabels){
t->setVisible(false);
}
}
void relasetext(){
wind->removeAllChildrenWithCleanup(true);
txton = false;
pLabels.clear();
}
bool showon(CCNode*pnode,int floor = 3){
CCNode* p = pnode->getChildByTag(id);
if (p){
p->setVisible(true);
}
else{
if (haveparent)return false;
haveparent = true;
pnode->addChild(wind,floor);
}
return true;
}
void hide(){
wind->setVisible(false);
}
bool deletefrom(CCNode*pnode){//彻底删去(之后确定很长时间不会再用到或者切换至别的场景要用到)
CCNode* p = pnode->getChildByTag(id);
if (p){
haveparent = false;
pnode->removeChild(p,true);
wind->release();
//此时需要重新创建:
wind = CCSprite::create("wind.png");
wind->setPosition(480,120);
}
else return false;
return true;
}
void set_txtspeed(float spd){
txtspeed = spd;
}
};
关于对话框的消除,每次用隐藏即可,只需要重新渲染文字即可。这样可以减少文件读取次数。文字的位置要根据对话框具体形状来计算,且每一行是有长度限制的(这里并没有给出限制)。 使用该类,效果如下: 可拓展内容: 文字以扫描式动画出现。 使用非单例以建立不同类型对话框,并绑定人物头像。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |