我有以下代码:
CCLabelTTF *start = [CCLabelTTF labelWithString:@"What Car was that?" fontName:@"Marker Felt" fontSize:32];
start.position = ccp( size.width * 1.5 / 4, size.height * 3 / 4 );
[start setOpacity:0.0];
[self addChild:start];
[self fadeText:start duration:1.5 curve:0 x:0 y:0 alpha:255.0];
以下是fadeText的定义:
- (void)fadeText:(CCLabelTTF *)progress duration:(NSTimeInterval)duration
curve:(int)curve x:(CGFloat)x y:(CGFloat)y alpha:(float)alpha
{
// Setup the animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
// The transform matrix
progress.opacity = alpha;
//[progress setOpacity:alpha];
// Commit the changes
[UIView commitAnimations];
}
由于某种原因,效果在没有动画的情况下发生,因此文本显示(因此不透明度变为255),但没有动画!
Alpha的范围从0到1。如果指定255,它将很快达到1并保持不变。所以只要通过alpha:1
就可以了。
您可以使用CCFadeTo
函数淡化CCLabelTTF
,而不是UIView动画。
id fade = [CCFadeTo actionWithDuration:1 opacity:200];
将此淡入淡出动画作为runAction赋予标签。