CCWave action on sprite - cocos2d-iphone

I'm trying to use CCWaves action, but it turns my screen into black, any suggestions to solve this problem?
id myWave = [CCWaves actionWithWaves:10 amplitude:7 horizontal:YES vertical:YES grid:ccg(25,20) duration:60];
[sprite runAction: [CCRepeatForever actionWithAction: myWave]];

In your AppDelegate, make sure your EAGL depthformat is 0:
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8 // kEAGLColorFormatRGBA8
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];

Related

Scene Change Crash in cocos2d

I am having a problem when trying to push a button and going to the next scene. I get the SIGABRT error. I don't know what the problem is:
[...]
//Play Button
CCMenuItem *playbutton;
playbutton = [CCMenuItemFont itemWithString:#"Play" target:self selector:#selector(playButtonMeathod:)];
CMenu *menu = [CCMenu menuWithItems:itemAchievement,playbutton,itemLeaderboard, nil];
[menu alignItemsHorizontallyWithPadding:20];
[menu setPosition:ccp( size.width/2, size.height/2 - 50)];
// Add the menu to the layer
[self addChild:menu];
-(void)playButtonMeathod{
// Create a scene transition that uses the "RotoZoom" effect
CCTransitionRotoZoom *transition = [CCTransitionRotoZoom transitionWithDuration:1.0 scene:[Level_1 scene]];
// Tell the director to run the transition
[[CCDirector sharedDirector] replaceScene:transition];
[...]
One issue might be that the method signature is incorrect in your CCMenuItemFont selector target. Try:
CCMenuItemFont *playbutton = [CCMenuItemFont itemWithString:#"Play" target:self selector:#selector(playButtonMeathod)];

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.

CCSprite comes and after 3 sec it'll remove automatically ?[Done]

I am working on project with cocos2d-android.
What I need this time is : A CCSprite comes on the screen and stay 3-4 sec and remove automatically. What class is available to do this work
If anybody have done this thing earlier. Suggest me the way ?
I'll give you code example in Objective-c cause i've never dealt with cocos2d-android, i believe it's pretty straightforward
CCSprite *spriteToDisplayAndRemove = [CCSprite spriteWithFile:#"filename.png"];
[self addChild:spriteToDisplayAndRemove];//say CCLayer adds our sprite
CCDelayTime *delay = [CCDelayTime actionWithDuration:3];
CCCallBlock *block = [CCCallBlock actionWithBlock:^{
[self removeChild:spriteToDisplayAndRemove];
}];
[self runAction:[CCSequence actions:delay, block, nil]];
EDIT:
Since blocks are unavailable in cocos2d-android you might use CCCallFunc instead. Again, Objective-c sample:
CCSprite *spriteToDisplayAndRemove = [CCSprite spriteWithFile:#"filename.png"];
spriteToDisplayAndRemove.tag = 100;
[self addChild:spriteToDisplayAndRemove];//say CCLayer adds our sprite
CCDelayTime *delay = [CCDelayTime actionWithDuration:3];
CCCallFunc *callFunc = [CCCallFunc actionWithTarget:self selector:#selector(removeSprite)];
[self runAction:[CCSequence actions:delay, callFunc, nil]];
And here is your removeSprite method:
-(void)removeSprite
{
CCSprite *sprite = [self getChildByTag:100];
[self removeChild:sprite];
}

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.

Cocos2d fade in/out action to repeat forever

I'm trying to make a method for my CCSprite based Player class to start the player instance fading in and out until stopped by calling stopAllActions.
In my Player class I have:
- (void)pulse
{
[self setOpacity:1.0];
CCAction *fadeIn = [CCFadeTo actionWithDuration:0.5 opacity:0.5];
CCAction *fadeOut = [CCFadeTo actionWithDuration:0.5 opacity:1.0];
CCSequence *pulseSequence = [CCSequence actions:
fadeIn, // I get a warning about incompatible pointer types...
fadeOut,
nil];
[self runAction:pulseSequence];
}
This doesn't work and doesn't address the repeat forever part. I know I should probably use CCRepeatForever but I'm not seeing how to implement it correctly.
Thanks!
I have not run this, but I think others have succeeded with something like:
- (void)pulse
{
[self setOpacity:1.0];
CCFadeTo *fadeIn = [CCFadeTo actionWithDuration:0.5 opacity:127];
CCFadeTo *fadeOut = [CCFadeTo actionWithDuration:0.5 opacity:255];
CCSequence *pulseSequence = [CCSequence actionOne:fadeIn two:fadeOut];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:pulseSequence];
[self runAction:repeat];
}
I had the same problem and it took me a loooong time to figure out why.
when you create CCSequences I found that you have to copy the CCAction.
In your case.
CCAction *fadeIn = [CCFadeTo actionWithDuration:0.5 opacity:0.5];
CCAction *fadeOut = [CCFadeTo actionWithDuration:0.5 opacity:1.0];
CCSequence *pulseSequence = [CCSequence actions:
[fadeIn copy],
[fadeOut copy],
nil];
Hope I helped.