2012-04-24 74 views
1

我需要你的建議。在我的GameScene中,我有一個點標籤curentPointsLabel。更新我有一個功能,但在性能updateCurentPoints(CCSequence)點標籤消失!爲什麼?CCLabelTTF在CCSequence後消失

在初始化

_curentPointsLabel = [CCLabelTTF labelWithString:@"" dimensions:CGSizeMake(screenSize.width*0.17f, screenSize.height*0.1f) alignment:UITextAlignmentLeft fontName:@"Trebuchet MS" fontSize:15]; 
_curentPointsLabel.anchorPoint = CGPointMake(1, 1); 
_curentPointsLabel.position = ccp(screenSize.width-screenSize.height/20, screenSize.height-2*screenSize.height/20); 
_curentPointsLabel.color = ccc3(0,0,0); 
[self addChild:_curentPointsLabel]; 
時,我想更新點標籤

我做

- (void)updateCurentPoints:(int)points { 

    if(_curentPoints+points<0) 
     _curentPoints=0; 
    else 
     _curentPoints=_curentPoints+points; 

    // [_curentPointsLabel setString:[NSString stringWithFormat:@"Score: %d",_curentPoints]]; 

    NSString * valueString=[[[NSString alloc] initWithString:[NSString stringWithFormat:@"Score: %d",_curentPoints]] retain]; 

    id sequence=[CCSequence actions: 
       [CCCallFuncND actionWithTarget: self selector: @selector(setLabelColor:withIndex:) data:(void*)1], 
       [CCCallFuncND actionWithTarget: self selector: @selector(setLabelValue:withValue:) data:(NSString*)valueString], 
       [CCBlink actionWithDuration:0.5f blinks:2], 
       [CCCallFuncND actionWithTarget: self selector: @selector(setLabelColor:withIndex:) data:(void*)3], 

       nil]; 

    [_curentPointsLabel runAction:sequence]; 

    [valueString release]; 
} 

//******************************************************************* 
-(void) setLabelValue:(id) sender withValue:(NSString*) value 
{ 
    CCLabelTTF *label=(CCLabelTTF *)sender; 
    NSString * valueString=[[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@",value]] retain]; 
    [label setString:[NSString stringWithFormat:@"%@",valueString]]; 
    [valueString release]; 
} 
//******************************************************************* 
-(void) setLabelColor:(id) sender withIndex:(int)index 
{ 
    CCLabelTTF *label=(CCLabelTTF *)sender; 

    if(index==0) 
     label.color = ccc3(255,255,255);//white 
    else if(index==1) 
     label.color = ccc3(0,255,0);//green 
    else if(index==2) 
     label.color = ccc3(255,0,0);//red 
    else if(index==3) 
     label.color = ccc3(0,0,0);//black 
    NSLog(@"color:%i",index); 
} 
+0

這與您的原始問題無關,但您在代碼中有'NSString'和冗餘'retain'的不必要的初始化。 – Hailei 2012-04-24 05:44:04

+0

您能否在整個過程中看到標籤閃爍?還是直接消失? – Hailei 2012-04-24 05:47:53

+0

是的,我可以看到,但是當眨眼後標籤消失 – 2012-04-24 09:48:50

回答

1

添加[CCShow行動],以確保精靈是可見的。嘗試:

id sequence=[CCSequence actions: 
      [CCCallFuncND actionWithTarget: self selector: @selector(setLabelColor:withIndex:) data:(void*)1], 
      [CCCallFuncND actionWithTarget: self selector: @selector(setLabelValue:withValue:) data:(NSString*)valueString], 
      [CCBlink actionWithDuration:0.5f blinks:2], 
      [CCShow action], 
      [CCCallFuncND actionWithTarget: self selector: @selector(setLabelColor:withIndex:) data:(void*)3], 
      nil]; 

不斷變化的眨眼次數從2到3(或其他奇數除了1)亦測試。任何一種解決方案都可以工作。

+0

非常感謝!這行得通!!! – 2012-04-25 05:49:18