在许多游戏中,当角色说话(对话)时,文本具有打字效果,看起来您正在观看角色键入文本。
对于使用 cocos2d 的 iPhone 游戏,实现这种外观和(简单)“动画”的好方法是什么?
如果有办法用 cocos2d 做到这一点很好,但我并不完全反对在 cocos2d 的 EAGL(OpenGL ES)视图之上分层 UIView 子类(UILabel?)。
在许多游戏中,当角色说话(对话)时,文本具有打字效果,看起来您正在观看角色键入文本。
对于使用 cocos2d 的 iPhone 游戏,实现这种外观和(简单)“动画”的好方法是什么?
如果有办法用 cocos2d 做到这一点很好,但我并不完全反对在 cocos2d 的 EAGL(OpenGL ES)视图之上分层 UIView 子类(UILabel?)。
我最终使用了内置的 UIKit 元素(UILabel)并将它们作为子视图添加到窗口中。
内置UIKit
不能很好地与cocos2d-built-in
UIKit
,如CCLabel,CCSprite
.
我建议使用 aCCLabelTTF
和CCAction
如下:
- (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];
}
}
这是未经测试的代码。此外,它假设该typeText
函数是在子类中实现的,CCNode
因为它调用[self runAction:]