在很多遊戲中,當某個角色說話(對話)時,文本具有打字效果,看起來您正在看文字類型的字符。iPhone中的文本打字效果cocos2d遊戲
對於使用cocos2d的iPhone遊戲,實現這種外觀和(簡單)「動畫效果」的好方法是什麼?
這很好,如果有辦法用cocos2d來做到這一點,但我並不完全反對在Cocos2d的EAGL(OpenGL ES)視圖上分層UIView子類(UILabel?)。
在很多遊戲中,當某個角色說話(對話)時,文本具有打字效果,看起來您正在看文字類型的字符。iPhone中的文本打字效果cocos2d遊戲
對於使用cocos2d的iPhone遊戲,實現這種外觀和(簡單)「動畫效果」的好方法是什麼?
這很好,如果有辦法用cocos2d來做到這一點,但我並不完全反對在Cocos2d的EAGL(OpenGL ES)視圖上分層UIView子類(UILabel?)。
我最終使用內置的UIKit元素(UILabel)並將它們作爲子視圖添加到窗口中。
內置的UIKit
無法與cocos2d-built-in
UIKit
,如CCLabel,CCSprite
。
我會建議使用CCLabelTTF
和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:]