Text Typing Effect in iPhone cocos2d Game
In many games, when a character is speaking (dialogue),开发者_高级运维 the text has a typing effect, where it looks like you are watching the character type the text.
What would be a good way to achieve this look and (simple) "animation" for an iPhone game which uses cocos2d?
It's good if there's a way to do it with cocos2d, but I'm not completely opposed to layering a UIView subclass (UILabel?) on top of cocos2d's EAGL (OpenGL ES) view.
I ended up using built-in UIKit elements (UILabel) and adding them as subviews to the window.
the built-in UIKit
could not works well with cocos2d-built-in
UIKit
,such as CCLabel,CCSprite
.
I would suggest using a CCLabelTTF
and CCAction
as follows:
- (void) typeText
{
static NSUInteger currentCharacter = 0;
// Your instance variable stores the text you want to "type" in it:
_text;
// Sorry, I can't remember substring functions, this is pseudo-code:
NSString *substring = [_text getSubstringFrom:0 to:currentCharacter];
// Also can't remember how to get length of a string (yipes)
NSUInteger stringLength = [_text length];
if (currentCharacter < stringLength)
{
CCSequence *loop = [CCSequence actions:[CCDelayTime actionWithDuration:0.3f],[CCCallFunc actionWithTarget:self selector:@selector(typeText)],nil];
[self runAction:loop];
}
}
This is untested code. Also, it assumes that the typeText
function is implemented in a subclass of CCNode
because it calls [self runAction:]
精彩评论