CCParticle define a region? - cocos2d-iphone

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];

Related

CCMenuItemSprite's alternative in Cocos2d v3

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.

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.

Why is my main menu image wrong sized on launch but not after a game?

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.

CCRepeatForever action stops automatically while switching scenes

I am developing a simple game app.
I am required to have two screens one on left & other on right, just like on scroll view with pagination having 2 screens to toggle between. I am detecting swipes with the help of RootViewController class and successfully swiping left and right screens.
But the problem is I have to have infinitely running rotateBy action on both screens each running this action on single sprite placed on center of screen.
I am using a main scene called SelectWorld & two are it's sub scenes.
First screen is called factory & other is called stack.
Following is my code:
Select World Screen:
#implementation SelectWorld
+(CCScene*)scene
{
CCScene* scene = [CCScene node];
SelectWorld* layer = [SelectWorld node];
[scene addChild:layer];
return scene;
}
-(id)init
{
if((self = [super init]))
{
[RootViewController singleton].swipeCallBackHandler = self;
[[RootViewController singleton] enableSwipeDetection];
factory = [[ColorfulFactory scene] retain];
stack = [[CoinsStack scene] retain];
[self addChild:factory];
isOnFactory = YES;
}
return self;
}
-(void)swipedLeft
{
if(isOnFactory)
{
[self removeAllChildrenWithCleanup:YES];
isOnFactory = NO;
CCTransitionScene* transitionalScene = [CCTransitionSlideInR transitionWithDuration:0.3 scene:stack];
[[CCDirector sharedDirector] replaceScene:transitionalScene];
}
}
-(void)swipedRight
{
if(!isOnFactory)
{
[self removeAllChildrenWithCleanup:YES];
isOnFactory = YES;
CCTransitionScene* transitionalScene = [CCTransitionSlideInL transitionWithDuration:0.3 scene:factory];
[[CCDirector sharedDirector] replaceScene:transitionalScene];
}
}
Here's the code for factory only, same goes for stack-
#implementation ColorfulFactory
+(CCScene*)scene
{
CCScene* scene = [CCScene node];
ColorfulFactory* layer = [ColorfulFactory node];
[scene addChild:layer];
return scene;
}
-(id)init
{
if((self = [super init]))
{
CGSize size = [CCDirector sharedDirector].winSize;
CCLabelTTF* info = [CCLabelTTF labelWithString:#"Colorful Factory" fontName:#"Helvetica" fontSize:35.0];
info.position = ccp(size.width/2, size.height-50);
[self addChild:info];
CCSprite* sprite = [CCSprite spriteWithFile:#"Icon.png"];
sprite.position = ccp(size.width/2, size.height/2);
[self addChild:sprite];
sprite.tag = 123;
CCRotateBy* rotateBy = [CCRotateBy actionWithDuration:60 angle:360.0];
CCRepeatForever* repeatForever = [CCRepeatForever actionWithAction:rotateBy];
[sprite runAction:repeatForever];
repeatForever.tag = 456;
}
return self;
}
Now the problem is, in first two swipes actions are running fine, but as soon as I try to swipe more than two or three times, actions got stopped. I haven't written a single line to stop this in both the classes. I require it there running forever.
Your mistake is that you're re-using the same scene object for every scene change. You can't do that in cocos2d, you have to create a new instance of the scene object.
You should never retain a scene object, since there ought to be only one active scene at any one time (with the exception of the duration of a scene transitioning to another scene - but that's handled by cocos2d internally).
For example:
CCScene* factory = [ColorfulFactory scene];
CCTransitionScene* transitionalScene = [CCTransitionSlideInR transitionWithDuration:0.3 scene:factory];
[[CCDirector sharedDirector] replaceScene:transitionalScene];
If you need both objects in memory, make them layers (or nodes), not scenes. Don't use replaceScene but instead animate the layers with regular moveTo actions in and out of the screen.

Resizing CCLayer. Not scaling

I have a CCLayer, that should be "unmasked" from top to bottom.
I implemented GLScissor pre-visit and post-visit to cut what is out of the layer bounds:
#pragma mark Clipping
-(void)visit {
[self preVisit];
[super visit];
[self postVisit];
}
-(void)preVisit {
if (!self.visible)
return;
glEnable(GL_SCISSOR_TEST);
CGRect clipRect = [self boundingBox];
CGPoint origin = [self convertToWorldSpaceAR:clipRect.origin];
CGRect scissorRect = CGRectMake(origin.x - self.anchorPoint.x * clipRect.size.width, 768 - origin.y - self.anchorPoint.y * clipRect.size.height, clipRect.size.width, clipRect.size.height);
scissorRect = CC_RECT_POINTS_TO_PIXELS(scissorRect);
glScissor((GLint) scissorRect.origin.x, (GLint) scissorRect.origin.y,
(GLint) scissorRect.size.width, (GLint) scissorRect.size.height);
}
-(void)postVisit {
glDisable(GL_SCISSOR_TEST);
}
And I make the CCLayer grow by setting a contentSize of 1024x0 and using a scheduled call:
-(void) grow {
float height = [self contentSize].height + 2;
if (height >= 768){
[self unschedule:#selector(grow)];
height = 768;
}
[self setContentSize:CGSizeMake(1024, height)];
}
This works like a charm. The CCLayer grows in height steadily and it is unmasked perfectly. However, I can't use any of the easing functions since I'm not using any action.
Is there any action that resizes the CCLayer (not scaling) that would allow me to use easing functions or is there any other (better) way to do this?
The best candidate is to use CCActionTween (formerly: CCPropertyAction) and wrap that in an ease action:
id tween = [CCActionTween actionWithDuration:5 key:"sizeHeight" from:1024 to:0];
id ease = [CCEaseInOut actionWithAction:tween];
[layer runAction:ease];
This will modify the property named sizeHeight of the layer. You can use that to adjust the contentSize, since you can't tween a C struct property like CGSize or CGPoint directly:
-(void) setSizeHeight:(float)height
{
self.contentSize = CGSizeMake(self.contentSize.width, height);
}