2

而不是在 HelloWorldLayer 的 init 方法中编写所有这些行:

 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];

我试图让标签永远改变颜色但卡住了:
我不知道在哪里放置相关命令+处理整数 x、y、z
我试图在更新方法中进行随机化过程,但没有任何访问标签,任何想法?

//  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
4

2 回答 2

3

如果您希望每次的 tint 颜色是随机的,那么您不能CCTintTo直接在 inside中使用CCRepeatForever。您需要重新随机化每个CCTintTo操作的 RGB 值。因此,您需要使用 block 将随机化过程嵌入到动作中。方法如下:

// 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);
于 2012-09-02T03:50:23.790 回答
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];
于 2012-09-02T02:46:03.037 回答