gpt4 book ai didi

objective-c - cocos2d ccTintTo,实现无限变化的颜色标签

转载 作者:行者123 更新时间:2023-12-04 18:13:06 24 4
gpt4 key购买 nike

而不是在 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

最佳答案

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

// 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);

关于objective-c - cocos2d ccTintTo,实现无限变化的颜色标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12231012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com