cocos2dx3.6实现打字效果

2023-08-24 13:00:24 浏览数 (1)

代码语言:javascript复制
	Label* laber = Label::create("", "Terminal", 20);
	this->addChild(laber);
	laber->setPosition(ccp(300,200));
	laber->setDimensions(300, 0);
	
	Vector<FiniteTimeAction*> m_vecAct;
	for (int i = 0; i <= (int)sText.length(); i  )
	{
		auto delay = DelayTime::create(0.3f);
		m_vecAct.pushBack(delay);
		std::string str = sText.substr(0, i);
		auto func = [laber, str]() throw() {laber->setString(str); };
		auto call = CallFunc::create(func);
		m_vecAct.pushBack((FiniteTimeAction*)call);
		
	}
	laber->runAction(Sequence::create(m_vecAct));

实现思路:每隔一段时间,label显示字符串的从0到当前位置的截断的子串(使用substr),即label是动作的执行者,每次只显示当前子串,然后延时,子串字符加1.

0 人点赞