CCMenuItemSprite's alternative in Cocos2d v3 - cocos2d-iphone

In Cocos2d 2.0 I used below code to use single image for normal and selected image with colour change on selection.
CCSprite *twitter_1 = [CCSprite spriteWithSpriteFrameName:FRAME_MM_TWR_1];
CCSprite *twitter_2 = [CCSprite spriteWithSpriteFrameName:FRAME_MM_TWR_2];
twitter_2.color = ccc3(128,128,128);
CCMenuItemSprite *twitterBtn = [CCMenuItemSprite itemWithNormalSprite:twitter_1
selectedSprite:twitter_2
target:self
selector:#selector(twitterBtnPress:) ];
In Cocos2d v3, I can use CCButton as alternative, but how to change selected frame colour?
CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCButton * twitterBtn = [CCButton buttonWithTitle:#""
spriteFrame:[cache spriteFrameByName:FRAME_MM_TWR_1]
highlightedSpriteFrame:[cache spriteFrameByName:FRAME_MM_TWR_1]
disabledSpriteFrame:nil];
twitterBtn = CCPositionTypeNormalized;
twitterBtn.position = ccp(0.5f, 0.5f);
[twitterBtn setTarget:self selector:#selector(playBtnPress:)];
[self addChild: twitterBtn];
Now in Cocos2d v3, how to use CCSprite for button and change colour?

You can use the method:
- (void) setBackgroundColor:(CCColor*)color forState:(CCControlState)state
of CCButton to set a different background color for the different states.

Related

CCParticle define a region?

I am trying to define a region to my snow, when its now in the rect of the screen, but my game using twice the height of he screen and so, i want it to start from 2*screenHeigh.
How to change that property ?
CCTexture2D *tex=[[CCTextureCache sharedTextureCache] addImage:#"snow.png"];
CCParticleSnow *emitter = [CCParticleSnow node];
[emitter setTexture:tex ];
emitter.scale=0.7;
emitter.speed = 120 + arc4random() %20;
[self addChild: emitter z:-1];

Issues with creating a CCParallaxNode in cocos2d?

I'm using a CCParallaxNode for a scrolling background in my GameLayer and I'm getting a bunch of errors and I can't figure out why.
"Initializer element is not a compile-time constant" and also "Unk
#implementation Background
// Load the sprites for each parallax layer, from background to foreground.
CCSprite* para1 = [CCSprite spriteWithFile:#"color.png"];
CCSprite* para2= [CCSprite spriteWithFile:#"ship-hd.png"];
CCSprite* para3 = [CCSprite spriteWithFile:#"twit.png"];
CCSprite* para4 = [CCSprite spriteWithFile:#"Start.png"];
// Set the correct offsets depending on the screen and image sizes.
para1.anchorPoint = CGPointMake(0, 1);
para2.anchorPoint = CGPointMake(0, 1);
para3.anchorPoint = CGPointMake(0, 0.6f);
para4.anchorPoint = CGPointMake(0, 0);
CGPoint topOffset = CGPointMake(0, screenSize.height);
CGPoint midOffset = CGPointMake(0, screenSize.height / 2);
CGPoint downOffset = CGPointZero;
// Create a parallax node and add the sprites to it.
CCParallaxNode* paraNode = [CCParallaxNode node];
[paraNode addChild:para1 z:1 parallaxRatio:CGPointMake(0.5f, 0) ositionOffset:topOffset];
[paraNode addChild:para2 z:2 parallaxRatio:CGPointMake(1, 0) positionOffset:topOffset];
[paraNode addChild:para3 z:4 parallaxRatio:CGPointMake(2, 0) positionOffset:midOffset];
[paraNode addChild:para4 z:3 parallaxRatio:CGPointMake(3, 0) positionOffset:downOffset];
[self addChild:paraNode z:0 tag:ParallaxSceneTagParallaxNode];
// Move the parallax node to show the parallaxing effect.
CCMoveBy* move1 = [CCMoveBy actionWithDuration:5 position:CGPointMake(−160, 0)];
CCMoveBy* move2 = [CCMoveBy actionWithDuration:15 position:CGPointMake(160, 0)];
CCSequence* sequence = [CCSequence actions:move1, move2, nil];
CCRepeatForever* repeat = [CCRepeatForever actionWithAction:sequence];
[paraNode runAction:repeat];
#end
You need to enclose your code in an -(id)init method, like this:
#implementation Background
- (id)init
{
if (self = [super init]) {
// Load the sprites for each parallax layer, from background to foreground.
CCSprite* para1 = [CCSprite spriteWithFile:#"color.png"];
// ... the rest of your code ... //
}
return self;
}
#end

Blur a CCLayerColor (for pause menu)

I'm curious if anyone knows of an already-implemented way to blur an entire CCLayer. I use a simple CCLayerColor set to black with a little opacity, but I would like to be able to blur the background enough to be indistinguishable blobs.
You can do this with CCLayerColor.
-(void)fadeBackground
{
ccColor4B color = {0,0,0,255};
CCLayerColor *fadeLayer = [CCLayerColor layerWithColor:color];
[self addChild:fadeLayer z:7];
fadeLayer.opacity = 0;
id fade = [CCFadeTo actionWithDuration:1.0f opacity:160];//200 for light blur
id calBlk = [CCCallBlock actionWithBlock:^{
//show pause screen buttons here
//[self showPauseMenu];
}];
id sequen = [CCSequence actions:fade, calBlk, nil];
[fadeLayer runAction:sequen];
}
Couldn't you just create a small tile that is translucent with some noise in it, and create a sprite that covers the screen where the texture parameters are set to repeat it?
CCSprite *blurSprite = [CCSprite spriteWithFile:#"blurtile.png" rect:CGRectMake(0, 0, 1024, 768)];
blurSprite.position = ccp(512,384);
ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
[blurSprite .texture setTexParameters:&params];
[self addChild:blurSprite];
I might have those params slightly wrong, but it should give the general idea.

How to Convert a CCSprite image to UIImage?

As the title, I have mentioned the code below
CCSprite *sprite = (CCSprite *)node;
CCTexture2D *texture2d = [sprite texture];
How can I get a UIImage *p from sprite?
In your case just add next methood into your class:
- (UIImage *) imageFromSprite :(CCSprite *)sprite
{
int tx = sprite.contentSize.width;
int ty = sprite.contentSize.height;
CCRenderTexture *renderer = [CCRenderTexture renderTextureWithWidth:tx height:ty];
sprite.anchorPoint = CGPointZero;
[renderer begin];
[sprite visit];
[renderer end];
return [renderer getUIImage];
}
how to use:
CCSprite *sprite = (CCSprite *)node;
UIImage *p = [self imageFromSprite:sprite]
uhh...
I think this is a better to way. Add this as a category on CCNode.
-(UIImage*)image {
CCRenderTexture* renderer = [CCRenderTexture renderTextureWithWidth:self.contentSize.width height:self.contentSize.height];
const CGPoint ANCHORBEFORE = self.anchorPoint;
self.anchorPoint = CGPointZero;
[renderer begin];
[self visit];
[renderer end];
self.anchorPoint = ANCHORBEFORE;
return [renderer getUIImage];
}
If you are using cocos2d 2.x, render sprite into CCRenderTexture and call getUIImage method of that render texture.
By looking at the source code of both CCSprite and CCTexture2D you can see that the image is used only for creating the texture data and the image object is not kept but temporarily used.
CCTexture2D source :
http://code.google.com/p/cocos2d-iphone/source/browse/trunk/cocos2d/CCTexture2D.m?r=1882
CCSprite source :
http://code.google.com/p/cocos2d-iphone/source/browse/trunk/cocos2d/CCSprite.m?r=1747
Look in the init methods that use CGImageRef

cocos2d Children sprite collision detection with parent sprite

Hi stackoverflow community!
How do you detect a children sprite collision with a parent sprite in cocos2d?
Currently I have my codes like this:
CGSize screenSize = [[CCDirector sharedDirector]winSize];
parentJumper = [CCSprite spriteWithFile:#"inviBtn.png"];
jumper = [CCSprite spriteWithFile:#"jumperRight.png"];
plat = [[Platform alloc]init];
plat = [Platform spriteWithFile:#"platformBlack.png"];
plat.position = ccp(160,100);
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0/60)];
jumper.anchorPoint = ccp(0.5, 0);
jumper.position = ccp(0, 20);
parentJumper.position = ccp(screenSize.width/2, 0);
[self addChild:plat];
[self addChild:parentJumper];
[parentJumper addChild:jumper];
Now how do I detect the collision between "jumper" & "plat"?
Thanks for your help!
Usually you can check collision like this:
if(CGRectIntersectsRect([jumper boundingBox], [plat boundingBox])) {
//Handle collision<br>
}