iphonecocos2d游戏中的文本输入效果

iphonecocos2d游戏中的文本输入效果,iphone,text,cocos2d-iphone,effect,Iphone,Text,Cocos2d Iphone,Effect,在许多游戏中,当角色说话(对话)时,文本具有打字效果,看起来就像是在观看角色键入文本 对于使用cocos2d的iPhone游戏,实现这种外观和(简单的)“动画”的好方法是什么 如果有一种方法可以用cocos2d实现这一点,那就好了,但我并不完全反对在cocos2d的EAGL(opengles)视图之上分层一个UIView子类(UILabel?)。我最终使用了内置的UIKit元素(UILabel)内置的UIKit无法与cocos2d内置的UIKit配合使用,例如CCLabel、CCSprite我建

在许多游戏中,当角色说话(对话)时,文本具有打字效果,看起来就像是在观看角色键入文本

对于使用cocos2d的iPhone游戏,实现这种外观和(简单的)“动画”的好方法是什么


如果有一种方法可以用cocos2d实现这一点,那就好了,但我并不完全反对在cocos2d的EAGL(opengles)视图之上分层一个UIView子类(UILabel?)。

我最终使用了内置的UIKit元素(UILabel)内置的
UIKit
无法与
cocos2d内置的
UIKit
配合使用,例如
CCLabel、CCSprite
我建议使用
cclabeltf
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];
    }
}
-(无效)类型文本
{
静态整数currentCharacter=0;
//实例变量存储要在其中“键入”的文本:
_文本;
//对不起,我不记得子字符串函数,这是伪代码:
NSString*substring=[\u text getSubstringFrom:0到:currentCharacter];
//也记不起如何获取字符串的长度(yipes)
NSU整数字符串长度=[_文本长度];
if(当前字符<字符串长度)
{
CCSequence*loop=[CCSequence actions:[CCDelayTime actionWithDuration:0.3f],[CCCallFunc actionWithTarget:self selector:@selector(typeText)],nil];
[自我运行:循环];
}
}
这是未经测试的代码。此外,它假定
typeText
函数是在
CCNode
的子类中实现的,因为它调用
[self runAction:]