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を処理する関連コマンド+を配置する場所がわかりません。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
4

2 に答える 2

3

色合いを毎回ランダムにしたい場合は、CCTintTo内で直接使用することはできませんCCRepeatForeverCCTintToアクションごとに 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);
于 2012-09-02T03:50:23.790 に答える
1

CCRepeatForeverアクションを見るべきです。名前が示すように、指し示すアクションを永遠に繰り返します。したがって、色を変更している update メソッドを削除し、以前のコードに戻って、それをアクション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 に答える