2012-09-01 67 views
2

而不是在HelloWorldLayer的init方法寫所有這些行:cocos2d的ccTintTo,實現無限變化的顏色標籤

CCTintTo* tint1 = [CCTintTo actionWithDuration:2 red:255 green:0 blue:0]; 
CCTintTo* tint2 = [CCTintTo actionWithDuration:2 red:0 green:0 blue:255]; 
.... 
CCSequence* sequence = [CCSequence actions:tint1, tint2, nil]; 
[label runAction:sequence]; 

我試圖使標籤變色永遠但得到stucked:
我不不知道在哪裏放置相關的命令+處理整數x,y,z
我試圖在update方法中執行隨機化過程,但沒有任何訪問權限的標籤,任何想法?

// HelloWorldLayer.h 
// Essentials 
// 
// Created by Steffen Itterheim on 14.07.10. 
// Copyright Steffen Itterheim 2010. All rights reserved. 
// 

#import "cocos2d.h" 

@interface HelloWorld : CCLayer 

{ 
    CCTintTo* tint1; 
    CCSequence* sequence1; 
    // CCLabelTTF* label; even tried property 
} 

// returns a Scene that contains the HelloWorld as the only child 
+(id) scene; 

@end 

// 
// HelloWorldLayer.m 
// Essentials 
// 
// Created by Steffen Itterheim on 14.07.10. 
// Copyright Steffen Itterheim 2010. All rights reserved. 
// 

#import "HelloWorldScene.h" 

#import "MenuScene.h" 
integer_t x; 
integer_t y; 
integer_t z; 

@implementation HelloWorld 


+(id) scene 
{ 
    CCScene* scene = [CCScene node]; 
    CCLayer* layer = [HelloWorld node]; 
    [scene addChild:layer]; 
    return scene; 
} 

-(id) init 
{ 
    if ((self = [super init])) 
    { 

     CCLOG(@"init %@", self); 

     // enable touch input 
     self.isTouchEnabled = YES; 

     CGSize size = [[CCDirector sharedDirector] winSize]; 


     // add the "touch to continue" label 
     CCLabelTTF* label = [CCLabelTTF labelWithString:@"Touch Screen For Awesome" fontName:@"AmericanTypewriter-Bold" fontSize:30]; 
     label.position = CGPointMake(size.width/2, size.height/8); 
     [self addChild:label]; 

     [self schedule:@selector(update:) interval:1/60.0f]; 
/* 
     tint1 = [CCTintTo actionWithDuration:2 red:x green:y blue:z]; 
     sequence1 = [CCSequence actions:tint1, nil ]; 
     id goaction=[CCRepeatForever actionWithAction:sequence1]; 
     [label runAction:goaction]; 

    */ 
    } 
    return self; 
} 

-(void) registerWithTouchDispatcher 
{ 
    // call the base implementation (default touch handler) 
    [super registerWithTouchDispatcher]; 
    //[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:YES]; 
} 

-(void) update:(ccTime)delta 
{ 

    x=(integer_t)(CCRANDOM_0_1()*255); y=(integer_t)(CCRANDOM_0_1()*255); z=(integer_t)(CCRANDOM_0_1()*255); 
    tint1 = [CCTintTo actionWithDuration:2 red:x green:y blue:z ]; 
    sequence1 = [CCSequence actions:tint1, nil ]; 
    [HelloWorld.label runAction:goaction]; //property label not found on object of type 'HelloWorld' 

} 

// Touch Input Events 
-(CGPoint) locationFromTouches:(NSSet *)touches 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchLocation = [touch locationInView: [touch view]]; 
    return [[CCDirector sharedDirector] convertToGL:touchLocation]; 
} 

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
} 

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGPoint location = [self locationFromTouches:touches]; 
    CCLOG(@"touch moved to: %.0f, %.0f", location.x, location.y); 
} 

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // the scene we want to see next 
    CCScene* scene = [MenuScene scene]; 
    CCTransitionSlideInR* transitionScene = [CCTransitionSlideInR transitionWithDuration:3 scene:scene]; 
    [[CCDirector sharedDirector] replaceScene:transitionScene]; 

} 

-(void) ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

} 


-(void) dealloc 
{ 
    CCLOG(@"dealloc: %@", self); 

    // always call [super dealloc] at the end of every dealloc method 
    [super dealloc]; 
} 
@end 

回答

1

你應該看看CCRepeatForever行動。顧名思義,它會重複它永遠指向的行動。所以,你應該刪除你在哪裏改變顏色的更新方法,並返回到CCSequence代碼,你有,並嵌入在一個CCRepeatForever行動:

CCTintTo* tint1 = [CCTintTo actionWithDuration:2 red:255 green:0 blue:0]; 
CCTintTo* tint2 = [CCTintTo actionWithDuration:2 red:0 green:0 blue:255]; 
.... 
CCSequence* sequence = [CCSequence actions:tint1, tint2, nil]; 
CCAction* repeat = [CCRepeatForever actionWithAction:sequence]; 
[label runAction:repeat]; 
3

如果你想色調的顏色是隨機的每個時間,那麼您不能直接在CCRepeatForever內使用CCTintTo。您需要重新隨機化每個CCTintTo操作的RGB值。因此,您需要通過使用塊在動作內嵌入隨機化過程。這裏是如何:

// do this in init method 

__block void (^changeTint)(CCNode*) = [[^(CCNode *node) { 
    GLubyte x = (integer_t)(CCRANDOM_0_1()*255), y = (integer_t)(CCRANDOM_0_1()*255), z = (integer_t)(CCRANDOM_0_1()*255); 
    [node runAction:[CCSequence actionOne:[CCTintTo actionWithDuration:2 red:x green:y blue:z] 
             two:[CCCallBlockN actionWithBlock:changeTint]]]; 
} copy] autorelease]; 

changeTint(label); 
+0

我有印象,他只是需要重複着色..無論哪種方式,你的回答確實隨機化色彩,但我看不出重複這個過程?他是否應該派遣一個GCD任務重複每個[色彩間隔時間]? – Mazyod

+1

@Mazyod該過程通過遞歸重複。 'changeTint'塊通過發送使用該塊作爲參數的'CCCallBlockN'遞歸自己。因此,那個'changeTint(label)'調用開始一個無限重複的過程。 – Lukman

+0

(+1)該死的,我太過分了。你教給我一些很棒的東西。謝謝。 (這不是真正的遞歸,對嗎?我認爲在這種情況下沒有遞歸棧。函數棧只是一個調用,AFAIK)。 – Mazyod