[cocos2d-x] Implementing dynamic text. How can I delete the text shown?

Started by
0 comments, last by ajay_tanwar 8 years ago

Greetings.

I have been working on creating a dynamic text that appears in the scene like the talking boxes of RPG games.

So far so good, but now I want to delete the text. I tried to use a vector to store each letter but I couldn't do it. This is my current code:


void GameScene::Introduction1(float dt)
{
	// we create the text that we will separate each letter to animate them one by one
	Label *set1 = Label::createWithTTF("Where am I?", "Marker Felt.ttf", 60);
	set1->setPosition(Point(400, 350));
	set1->setOpacity(0);
	addChild(set1, 4);

	auto WChar = (Sprite *)set1->getLetter(currentLetter);
	auto jump = JumpBy::create(0.5f, Vec2::ZERO, 60, 1);
	WChar->setOpacity(255);
	WChar->runAction(jump);
	
	currentLetter++;

	if (currentLetter >= set1->getStringLength()) // if the last word is shown
	{
		for (int i = currentLetter; i > 0; i--)//I added this for to go through each letter
		{
			unschedule(schedule_selector(GameScene::Introduction1)); // these 2 lines outside ...
			currentLetter = 0; // ... the 'for' works perfectly
			// currentLetter--; // I added this for the use of the 'for'
		}
		return;
	}
}

I added that 'for' to look for a way to store each letter in a vector and then assign them the opacity to 0. I deleted the code for that so I don't get confused.

Any suggestion about it will be awesome. Is C++ so with a help on the code logic I can easily translate it for Cocos.

Thanks everyone.

Advertisement

Try this :

Since its a scheduled method don't add Label every time it's called .

Add it just once

Label *set1; // in header

set1 = nullptr ;// you could do this before scheduling the method.

void GameScene::Introduction1(float dt)
{
// we create the text that we will separate each letter to animate them one by one
if(!set1)
{
set1= Label::createWithTTF("Where am I?", "marker Felt.ttf", 60);
set1->setPosition(Point(400, 350));
set1->setOpacity(0);
addChild(set1, 4);
}
auto WChar = (Sprite *)set1->getLetter(currentLetter);
auto jump = JumpBy::create(0.5f, Vec2::ZERO, 60, 1);
WChar->setOpacity(255);
WChar->runAction(jump);
currentLetter++;
if (currentLetter >= set1->getStringLength()) // if the last word is shown
{
float dt = 0;
unschedule(schedule_selector(MainMenuScene::Introduction1)); // these 2 lines outside ...
for (int i = currentLetter-1; i >= 0; i--)//I added this for to go through each letter
{
auto WChar = (Sprite *)set1->getLetter(i);
auto remove_ = Sequence::create(DelayTime::create(dt) , JumpBy::create(0.5f, Vec2::ZERO, 60, 1), RemoveSelf::create() , nullptr);
WChar->runAction(remove_);
dt += .02f; //dt is added to just add delay to the effect.
}
currentLetter = 0;
}
}

Hope this helps.

This topic is closed to new replies.

Advertisement