Fade in/Fade out in Cocos 2d not working - iphone

I'm trying to make a fade in-fade out effect in cocos 2d, using a black png image. Its to make a better way to show something, instead of the casual way...
Is something wrong on this code?
- (void) effectFade {
CCSprite *effectScreen = [CCSprite spriteWithFile:#"black-iphone.png"];
effectScreen.opacity = 0;
[effectScreen runAction:[CCSequence actions:
[CCFadeIn actionWithDuration:1],
[CCCallFunc actionWithTarget:self selector:#selector(hideOutShowIn)],
[CCFadeOut actionWithDuration:1],
[CCCallFunc actionWithTarget:self selector:#selector(finish)],
nil]];
CCFadeTo *fadeIn = [CCFadeIn actionWithDuration:1];
CCFadeTo *fadeOut = [CCFadeOut actionWithDuration:1];
CCSequence *fadeSequence = [CCSequence actionOne:fadeIn two:fadeOut];
}

You don't specify what is not working as expected, but it seems to me that you are not adding effectScreen to any layer or other node, so that it can be displayed.
As to the rest, the code seems correct to me (aside from fadeIn, fadeOut, fadeSequence that are not used).
EDIT:
you are definitely using effectScreen in an ObjC/C sense; what you are not doing is adding it to your scene by doing something like:
[self addChild:effectScene];
(assuming self is your scene or another CCNode class).
The thing about not using fadeSequence is a bit different, since you are not using it in the ObjC/C sense: you define it and never ever reference it again.

Related

Cocos2D checking if sprite is off screen?

Im using this code to fire upwards:
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent* )event
{
//Spawn the bullet
CCSprite * projectile = [CCSprite spriteWithFile:#"Projectile.png" rect:CGRectMake(0, 0, 17.5, 10)];
projectile.position = ccp(donk.position.x , 50);
[self addChild:projectile];
//Actualy Fire
[projectile runAction: [CCMoveTo actionWithDuration:.2 position: ccp (donk.position.x , 350)]];
}
It works like I want but how can i use an if statement to check if the projectile it is off the top off the screen so I can remove it?
I tried using this:
if (projectile.position.y >= 330) {
CCLOG(#"Removed");
[self removeChild:projectile cleanup:YES];
}
but I forgot that touches ended is only called once.
Think about when you should be checking whether the bullet is offscreen. Not at the same instant it was fired, right?
There are many ways to do this.
You have a moveTo action already. CCActions can have callbacks that tell you when they're completed; see the header.
You can define an update: method and enable it with scheduleUpdates to be able to test the position every frame.
If you use a physics engine, you can make a "wall" for the edge of the screen and remove the bullet in response to collision detection.
There are probably other ways, too. Look into what the framework provides.
What about this:
[projectile runAction:[CCSequence actions:
[CCMoveTo actionWithDuration:.2 position:ccp(donk.position.x,350)],
[CCCallBlock actionWithBlock:^{
[projectile removeFromParentAndCleanup:YES];
}],
nil]];
(not tested)

dealloc doesn't work in cocos2d

i have game with cocos2d and method dealloc
i use this to change scene.
CGSize size = [[CCDirector sharedDirector] winSize];
CCMoveTo* move = [CCMoveTo actionWithDuration:1.0f position:CGPointMake(-(size.width), 0)];
CCEaseBackInOut* ease = [CCEaseBackInOut actionWithAction:move];
CCCallFunc* func = [CCCallFunc actionWithTarget:self selector:#selector(changeScene:)];
CCSequence* sequence = [CCSequence actions:ease, func, nil];
[self runAction:sequence];
or this in another scene.
CCScene* scene = [levelScene scene];
[userName removeFromSuperview];
CCTransitionFlipAngular *transitionScene=[CCTransitionFlipAngular transitionWithDuration:1 scene:scene];
[[CCDirector sharedDirector] replaceScene:transitionScene];
whene i changing scene the dealloc method doesn't do anything and onExit method too.
where i can releas my pointers ?or i have and constants wnat to equal to 0 when change scene.
whene i can do it?or why i can use dealloc.
dealloc is only called on an object when the memory management system determines that the object is no longer in use, when the retain count goes to zero. What are you doing to cause that to happen? I don't see any release or autorelease methods in your code samples.
You're going to have to be more specific about what you're doing to change scenes for a better answer.

how to implement two different animations on same sprite in cocos2d

I'm confused on using animations in cocos2d... I've a sprite which has 3 types of animations, like a smiley which laughs, cries and winks eyes... And I have separate sprite sheets for each of these animations... how will I be able to use these animations on the same sprite... Can anybody help me please???
Regards,
Suraj
It would be much easier to have all your animations on the same sprite sheet, as, if your sprite is using a CCBatchnode to do it's draw method, you'd have remove it as a child from 1 sheet, and readd it to another.
In your CCSprite subclass, set some CCAction's as instance variables.
In an initialization method, write those actions and store them to the instance variables.
Then when you want to use an animation, tell your sprite to run it.
E.g.
NSMutableArray *smileFrames = [NSMutableArray array];
[smileFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:#"character_smile.png"]];
[smileFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:#"character_smile2.png"]];
[smileFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:#"character_smile3.png"]];
CCAnimation *smileAnim = [CCAnimation
animationWithFrames:smileFrames delay:0.2f];
self.smileAction = [CCSequence actions:
[CCAnimate actionWithAnimation:smileAnim restoreOriginalFrame:NO],
[CCCallFunc actionWithTarget:self selector:#selector(smileFinished)],
nil];
Then you would simply use..
[sprite runAction:smileAction];
I have added a CCCallFunc to the end of the animation, as you may want to revert back to an idle animation after it is finished.
Don't forget to release any retained actions when the sprite is deallocated.

How to make cocos2d sprite scale up and down (for a pulsating effect) every second?

I want to have a sprite scale up and down once every second to make it seem like it is bulging and pulsating. How can I do this?
As the post before contains syntax errors. To be more precise, I post working code:
CCSprite * sprite = ...; // create the sprite.
id scaleUpAction = [CCEaseInOut actionWithAction:[CCScaleTo actionWithDuration:1 scaleX:1.0 scaleY:1.0] rate:2.0];
id scaleDownAction = [CCEaseInOut actionWithAction:[CCScaleTo actionWithDuration:0.5 scaleX:0.8 scaleY:0.8] rate:2.0];
CCSequence *scaleSeq = [CCSequence actions:scaleUpAction, scaleDownAction, nil];
[sprite runAction:[CCRepeatForever actionWithAction:scaleSeq]];
You could use a simple [CCScaleTo ..] action or if you want to create your own "effect" you could advance the CCFiniteTimeAction. I would prefer the first one :
CCSprite * sprite = ...; // create the sprite.
sprite.anchorPoint = ccp( 0.5, 0.5 ); center the pivot
id myAction = [CCRepeatForEver actionWithActions:[CCScaleTo actionWithDuration:0.5 scaleX:2.0 ScaleY:2.0],[CCScaleTo actionWithDuration:0.5 scaleX:0.5 ScaleY:0.5], nil];
[sprite runAction:myAction];
use CCEase to make the animation nonlinear
id myAction = [CCRepeatForEver actionWithActions:[CCEaseInOut actionWithAction:[CCScaleTo actionWithDuration:0.5 scaleX:2.0 ScaleY:2.0] rate:2.0],[CCEaseInOut actionWithAction:[CCScaleTo actionWithDuration:0.5 scaleX:0.5 ScaleY:0.5] rate:2.0], nil];
this post may contain errors. but I hope you understand the way to come to the goal.

What is the best way to make a bouncing ball animation with infinite loop on iPhone?

I am developing an iPhone game in which birds bounce.
I have set up the images for animating the wings of the flying bird like this:
[imgBird[i] setAnimationImages:birdArrayConstant];
[imgBird[i] setAnimationDuration:1.0];
[imgBird[i] startAnimating];
Now how I make the bird move is to use an NSTimer to fire every 0.03 seconds which adds/subtracts 1 from the x or y coordinate from imgBird[i].center.
I learnt about doing it like this from here. http://icodeblog.com/2008/10/28/iphone-programming-tutorial-animating-a-ball-using-an-nstimer/
But the issue is the birds slow down as soon as another timer (for moving my ship the same way) fires and returns back to original speed as i stop moving the ship.
Is there a better way to keep the bird moving except NSTimer?
The movement of bird is an infinite loop.
You will need to import CoreGraphics and the QuartzCore frameworks into you project.
Add these lines to the top of your file.
#import <QuartzCore/QuartzCore.h>
#import <CoreGraphics/CoreGraphics.h>
...
UIImageView *bird = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bird2.png"]];
CALayer *b = bird.layer;
// Create a keyframe animation to follow a path to the projected point
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"scale"];
animation.removedOnCompletion = NO;
// Create the path for the bounces
CGMutablePathRef thePath = CGPathCreateMutable();
// Start the path at my current location
CGPathMoveToPoint(thePath, NULL, bird.center.x, bird.center.y);
CGPathAddLineToPoint(thePath, NULL,20, 500.0);
/* // very cool path system.
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath,NULL,74.0,74.0);
CGPathAddCurveToPoint(thePath,NULL,74.0,500.0,
320.0,500.0,
320.0,74.0);
CGPathAddCurveToPoint(thePath,NULL,320.0,500.0,
566.0,500.0,
566.0,74.0);
*/
//animation.rotationMode = kCAAnimationRotateAuto;
animation.path = thePath;
animation.speed = 0.011;
//animation.delegate = self;
animation.repeatCount = 1000000;
// Add the animation to the layer
[b addAnimation:animation forKey:#"move"];
Hope that helps a bit.
From your question it seems you are using more than one timer. Use just one to animate all your objects. Somewhere, when your app starts, have this:
[NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:#selector(mainLoop:) userInfo:nil repeats:YES];
Then, in main loop, something like this:
- (void)mainLoop:(NSTimer *)timer
{
// compute new position for imgBirds
for (int i=0; i<kBirdCount; i++)
{
imgBirds[i].center = CGPointMake(somex, somey);
}
// compute new position for ship
ship.center = CGPointMake(somex, somey);
}
Also, you should compute somex and somey as a function of your timer interval. In that way, if you change it, your animation will still look the same.
You'll want to use Core Animation instead of manually moving the birds. Take a look at CAAnimation in the docs. You basically set up an animation, the path you want it to move along, and just tell it to run. It'll take care of the rest. It also has support for easing which will make the pace slow down and accelerate so it looks more natural.