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:¶ms];
[self addChild:blurSprite];
I might have those params slightly wrong, but it should give the general idea.
Related
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.
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];
I have an image that loads on a MainMenu scene and appears as wrong sized when the game launches but then after finishing a level returns you to the MainMenu again the image appears right sized.
The first time the image appears to be three quarters or 4/5 of the size of the screen from left to right. The image has a white background around it so I can see a black tall rectangle in the right side of the screen on the main menu's first launch. But after a game it is sized properly and the white background image is sized properly.
Anybody ever have this happen?
Here is the init code for the MainMenuLayer:
-(id)init {
if( (self=[super initWithColor:ccc4(255,255,255,255)]) ) {
[[GameManager sharedGameManager] playBackgroundTrack:BACKGROUND_TRACK_MAIN_MENU];
CGSize screenSize = [CCDirector sharedDirector].winSize;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
CCSprite *background = [CCSprite spriteWithFile:#"MainMenu-iPad.png"];
[background setPosition:ccp(screenSize.width/2,screenSize.height/2)];
[self addChild:background];
[self displayMainMenu];
} else {
CCSprite *background = [CCSprite spriteWithFile:#"MainMenu.png"];
[background setPosition:ccp(screenSize.width/2,screenSize.height/2)];
[self addChild:background];
[self displayMainMenu];
}
-
It works fine on the simulator though...
Print screen position, if position is wrong then put you code in onEnter instead of init.
See this thread in Stack overflow.
-(id)init {
if( (self=[super initWithColor:ccc4(255,255,255,255)]) ) {
}
return self;
}
-(void)onEnter {
[super onEnter];
[[GameManager sharedGameManager] playBackgroundTrack:BACKGROUND_TRACK_MAIN_MENU];
CGSize screenSize = [CCDirector sharedDirector].winSize;
CCSprite *background = [CCSprite spriteWithFile:#"MainMenu.png"];
[background setPosition:ccp(screenSize.width/2,screenSize.height/2)];
[self addChild:background];
[self displayMainMenu];
}
The image was apparently being cached even after clean and build! I ended up replacing the image by removing the original and adding the same one but with some markers just to make sure it got replaced.
I've got an animation of a man walking. The background image contains some trees. I've a mask with those trees and I want to apply it over the man, so it'll look like that the man walks behind the trees.
This is my code:
CCRenderTexture * rt = [CCRenderTexture
renderTextureWithWidth:maskSprite.contentSizeInPixels.width
height:maskSprite.contentSizeInPixels.height];
rt.position = ccp(maskSprite.contentSize.width/2, maskSprite.contentSize.height/2);
[rt.sprite setBlendFunc: (ccBlendFunc) { GL_ONE_MINUS_SRC_ALPHA, GL_ZERO }];
[rt beginWithClear:0.0f g:0.0f b:0.0f a:1.0f];
[walkingManSprite visit];
[rt end];
[maskSprite setBlendFunc: (ccBlendFunc) { GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }];
[rt beginWithClear:0.0f g:0.0f b:0.0f a:1.0f];
[maskSprite visit];
[rt end];
CCSprite *retval = [CCSprite spriteWithTexture:rt.sprite.texture];
retval.flipY = YES;
return retval;
The result is that the man walks behinds the trees, but my mask is being shown as a black shadow over the background. How can I not show the mask but apply it over the animation?
I have an app where I have several layers created from PNG images with transparency. These layers are all on the screen over each other. I need to be able to ignore touches given to transparent areas of layers and just be able to detect as touches, when the user taps on a non-transparent area of a layer... see pic...
How do I do that? thanks.
Here you have a possible solution.
Implement an extension on CCLayer and provide this method:
- (BOOL)isPixelTransparentAtLocation:(CGPoint)loc
{
//Convert the location to the node space
CGPoint location = [self convertToNodeSpace:loc];
//This is the pixel we will read and test
UInt8 pixel[4];
//Prepare a render texture to draw the receiver on, so you are able to read the required pixel and test it
CGSize screenSize = [[CCDirector sharedDirector] winSize];
CCRenderTexture* renderTexture = [[CCRenderTexture alloc] initWithWidth:screenSize.width
height:screenSize.height
pixelFormat:kCCTexture2DPixelFormat_RGBA8888];
[renderTexture begin];
//Draw the layer
[self draw];
//Read the pixel
glReadPixels((GLint)location.x,(GLint)location.y, kHITTEST_WIDTH, kHITTEST_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
//Cleanup
[renderTexture end];
[renderTexture release];
//Test if the pixel's alpha byte is transparent
return (pixel[3] == 0);
}
if Lio's solution doesn't work, you may add add transparent sprite as a child you yours, place it just under your non-transparent area with size of this non-tranparent area and resive all touches by this new transparent sprite, but not by original sprite.
Here is my solution to your requirement, let me know if it works or not
Create a Category on CCMenu with Name Transparent
File CCMenu+Tranparent.h
#import "CCMenu.h"
#interface CCMenu (Transparent)
#end
File CCMenu+Tranparent.m
#import "CCMenu+Transparent.h"
#import "cocos2d.h"
#implementation CCMenu (Transparent)
-(CCMenuItem *) itemForTouch: (UITouch *) touch{
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
CCMenuItem* item;
CCARRAY_FOREACH(children_, item){
UInt8 data[4];
// ignore invisible and disabled items: issue #779, #866
if ( [item visible] && [item isEnabled] ) {
CGPoint local = [item convertToNodeSpace:touchLocation];
/*
TRANSPARENCY LOGIC
*/
//PIXEL READING 1 PIXEL AT LOCATION
CGRect r = [item rect];
r.origin = CGPointZero;
if( CGRectContainsPoint( r, local ) ){
if([NSStringFromClass(item.class) isEqualToString:NSStringFromClass([CCMenuItemImage class])]){
CCRenderTexture* renderTexture = [[CCRenderTexture alloc] initWithWidth:item.boundingBox.size.width * CC_CONTENT_SCALE_FACTOR()
height:item.boundingBox.size.height * CC_CONTENT_SCALE_FACTOR()
pixelFormat:kCCTexture2DPixelFormat_RGBA8888];
[renderTexture begin];
[[(CCMenuItemImage *)item normalImage] draw];
data[3] = 1;
glReadPixels((GLint)local.x,(GLint)local.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data);
[renderTexture end];
[renderTexture release];
if(data[3] == 0){
continue;
}
}
free(data);
return item;
}
}
}
return nil;
}
#end
This will check for pixel for returning the CCMenuItem.
Its working fine here.. let me know if you face any issues
-Paresh Rathod
Cocos2d Lover
The solution that worked great for me was using Sprite sheets. I use TexturePacker to create sprite sheets. Steps to create sprite sheet using TexturePacker:
1. Load all the image (.png) files into TexturePacker.
2. Chose data format as coco2d and choose PVR as the texture format.
3. Load the sprite sheet into your code and extract images from your sprite sheet.
Detailed description can be found here.