I added the following code to the Cocos2D default template's IntroScene:
CCButton *playButton = [CCButton buttonWithTitle:#"Play" fontName:#"HelveticaNeue" fontSize:18.0f];
[playButton setBackgroundColor:[CCColor colorWithWhite:0.7f alpha:1.0f] forState:CCControlStateNormal];
[playButton setBackgroundColor:[CCColor colorWithWhite:0.75f alpha:1.0f] forState:CCControlStateHighlighted];
playButton.positionType = CCPositionTypeNormalized;
playButton.position = ccp(0.5f, 0.35f);
playButton.zoomWhenHighlighted = NO;
playButton.preferredSize = CGSizeMake(222, 46);
[playButton setTarget:self selector:#selector(onSpinningClicked:)];
[self addChild:playButton];
For some reason the button has no background color. Any ideas?
Because you didn't set a color, only brightness (this is what "white" means in the context of CCColor).
Try using a color with this initializer:
CCColor* normalColor = [CCColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0f]
[playButton setBackgroundColor:normalColor forState:CCControlStateNormal];
This will make the button background red.
Related
Here are my code how I am trying to draw an image and a hole on it. (The code is from the cocos2d test project)
CCSprite *target = [CCSprite spriteWithFile:#"blocks.png"];
target.anchorPoint = CGPointZero;
target.scale = 3;
CCClippingNode *outerClipper_ = [[CCClippingNode clippingNode] retain];
outerClipper_.contentSize = CGSizeApplyAffineTransform(target.contentSize, CGAffineTransformMakeScale(target.scale, target.scale));
outerClipper_.anchorPoint = ccp(0.5, 0.5);
outerClipper_.position = ccpMult(ccpFromSize([CCDirector sharedDirector].winSize), 0.5);
outerClipper_.stencil = target;
CCClippingNode *holesClipper = [CCClippingNode clippingNode];
holesClipper.inverted = YES;
holesClipper.alphaThreshold = 0.05;
[holesClipper addChild:target];
CCNode *holes_ = [[CCNode node] retain];
[holesClipper addChild:holes_];
CCNode *holesStencil_ = [[CCNode node] retain];
holesClipper.stencil = holesStencil_;
[outerClipper_ addChild:holesClipper];
[self addChild:outerClipper_ z:9999];
// Add the hole
CCSprite *hole = [CCSprite spriteWithFile:#"hole_effect.png"];
hole.position = [outerClipper_ convertToNodeSpace:ccpMult(ccpFromSize([CCDirector sharedDirector].winSize), 0.5)];
[holes_ addChild:hole];
CCSprite *holeStencil = [CCSprite spriteWithFile:#"hole_stencil.png"];
holeStencil.position = [outerClipper_ convertToNodeSpace:ccpMult(ccpFromSize([CCDirector sharedDirector].winSize), 0.5)];
[holesStencil_ addChild:holeStencil];
All the images can be found in the cocos2d test project.
The problem is that the images appear, but there is no hole on it. What I am doing wrong?
The problem was that I didn't setup my CCGLView correctly. I have to setup the depth format to GL_DEPTH24_STENCIL8_OES instead of the value 0.
I need to put some CCsprites on screen that later on will be fade in to screen.
I cant hide them ,because the CCFade action will not work on hidden sprite , or a sprite with opacity=0 .
What i do is put them on screen and fade them out :
[colors[i] runAction:[CCFadeOut actionWithDuration:0]];
[self addChild:colors[i] z:0];
it turned out that fading out in zero time is not unseen, so they appear for a second the moment i add them to CCScene.
How would i put them on screen to be unseen, and than fade them in with CCFadeIn action ?
You can stack actions using a sequence. See the example below from one of my projects :
CCSprite *frame1 = [CCSprite spriteWithSpriteFrame:[frames objectAtIndex:0]];
frame1.flipX = self.flipX;
frame1.scale = self.scaling;
frame1.visible = NO;
frame1.opacity = 255;
frame1.rotation = self.rotation;
frame1.position = self.offset;
animation = [CCAnimation animationWithSpriteFrames:frames delay:(duration / self.numberOfFrames)];
id stall = [CCDelayTime actionWithDuration:delay];
id show = [CCShow action];
id animate = [CCAnimate actionWithAnimation:animation];
id hide = [CCHide action];
id clean = [CCCallBlock actionWithBlock:^{
[frame1 removeFromParentAndCleanup:YES];
}];
id enchiladas = [CCSequence actions:stall, show, animate, hide, clean, nil];
[node addChild:frame1 z:5];
[frame1 runAction:enchiladas];
Similar thing. I want to run an animation that will appear after a set delay time, then vanish and cleanup after itself when complete.
YOU can use sprite.opacity =0; initially
and in actions you can increase the opacity
Suppose, I want to apply blue color to fade-in and fade-out image, how can i achieve this?
Fade in and fade out are controlled by the delegate method:
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value;
This method is used to customise the parameters of the standard carousel types. By implementing this method, you can tweak options such as:
iCarouselOptionFadeMin
iCarouselOptionFadeMax
iCarouselOptionFadeRange
These three options control the fading out of carousel item views based on their offset from the currently centered item. FadeMin is the minimum negative offset an item view can reach before it begins to fade. FadeMax is the maximum positive offset a view can reach before if begins to fade. FadeRange is the distance over which the fadeout occurs, measured in multiples of an item width (defaults to 1.0).
So for example fade out from 0.5f to 2.5f:
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value {
switch (option) {
case iCarouselOptionFadeMax:
return 0.5f;
break;
case iCarouselOptionFadeMin:
return -0.5f;
break;
case iCarouselOptionFadeRange:
return 2.5f;
break;
default:
return value;
break;
}
}
Now- to apply the blue color to fade-in and fade-out image you could subclass iCarousel. The following example assumes that as an image fades out (alpha approaches 0.0f), the blue color overlay fades in (alpha approaches 1.0f) using a cosine function.
#import "iCarousel.h"
// Done to suppress compiler warning to super
#interface iCarousel (BlueOverlay)
- (UIView *)containView:(UIView *)view;
- (void)transformItemView:(UIView *)view atIndex:(NSInteger)index;
- (CGFloat)alphaForItemWithOffset:(CGFloat)offset;
#end
#interface BlueCarousel : iCarousel
#end
#implementation BlueCarousel
- (UIView *)containView:(UIView *)view
{
//get container frame
UIView *containerView = [super containView:view];
CGRect frame = containerView.frame;
//create the blue overlay layer
UIView *overlayView = [[UIView alloc] initWithFrame:frame];
overlayView.backgroundColor = [UIColor blueColor];
overlayView.alpha = 0.0f;
[containerView addSubview:overlayView];
return containerView;
}
- (void)transformItemView:(UIView *)view atIndex:(NSInteger)index
{
[super transformItemView:view atIndex:index];
//calculate offset
CGFloat offset = [self offsetForItemAtIndex:index];
//update blue overlay alpha e.g. using cosine function
CGFloat alpha = cosf(M_PI_2*[self alphaForItemWithOffset:offset]);
[(UIView*)[view.superview.subviews lastObject] setAlpha:alpha];
}
#end
I'm trying to implement a highlight animation to my sprites. The sprite should highlight to a given color and gradually reverse back to its original colors, with the following code:
- (void)highlight {
CCTintTo *tintAction = [CCTintTo actionWithDuration:0.1 red:255 green:255 blue:255];
CCTintTo *tintBackAction = [tintAction reverse];
CCSequence *sequence = [CCSequence actions: tintAction, tintBackAction, nil];
[self runAction:sequence];
}
Now this function raises an exception as CCTintTo doesn't seem to implement 'reverse', which makes sense. Is there any other way to implement removal of an added tint over an interval, using a CCAction?
CCSprite's default color is ccWhite, {255, 255, 255}, so if you
want to make sprite lighter, you'll have to subclass CCSprite/write shader to use additive coloring.
Just tint it back:
CCColor3B oldColor = sprite.color;
CCTintTo *tintTo = [CCTintTo actionWithDuration:t red:r green:g blue:b];
CCTintTo *tintBack = [CCTintTo actionWithDuration:t red:oldColor.r green:oldColor.g blue:oldColor.b];
[sprite runAction:[CCSequence actions: tintTo, tintBack, nil]];
You can store previous color before start tint, then just create CCTintTo with initial RGB values.
For Cocos2dx (C++)
ccColor3B oldColor = sprite->getColor();
CCTintTo* action = CCTintTo::create(0.5f, 127, 255, 127);
CCTintTo* actionReverse = CCTintTo::create(0.5f, oldColor.r, oldColor.g, oldColor.b);;
sprite->runAction(CCSequence::create(action, actionReverse, NULL));
Works fine Kreiri, thanks! I already gave a plus to you:).
I think I'm just understanding scaling/positioning nodes/layers incorrectly. I'm setting up my node like this (node class is derived from CCNode):
-(id) init
{
if ((self = [super init]))
{
// Create parallax background node.
background = [BackgroundNode node];
[self addChild:background z:0];
// Create foreground node.
foreground = [ForegroundLayer node];
[self addChild:foreground z:0];
self.position.y = 500.0f;
self.scaleX = 1.5f;
self.scaleY = 1.5f;
}
return self;
}
It doesn't seem to matter what I set the self.position.y to - the scaled node is always displayed as though it was positioned in the bottom-left of the screen. I've tried playing around with anchorPoint as well, but it doesn't really seem to change anything.
The reason I'm asking is because I'd like to be able to pan vertically when I'm zoomed in - this doesn't seem to really be the right way to accomplish it, though. Any ideas?
self.position.y = 500.0f;
doesn't work. It should be
self.position = ccp(self.position.x, 500.0f);
Please refer to "Cocoa Objective-c Property C structure assign fails".