Card Flip effect - cocos2d-iphone

I’m starting my development in Cocos2D and I wish to make this card effect you can see on the 0:26 second in this movie:
http://www.youtube.com/watch?v=7gn7Obeykm8
Is there any special Cocos2D Transition for this effect?

Looks like CCTransitionFlipX
http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_transition_flip_x.html

CCFlipX3D* flipx = CCFlipX3D::create(1);
CCActionInterval* flipx_back = flipx->reverse();
CCDelayTime* delay = CCDelayTime::create(0);
node->runAction(CCSequence::create(flipx, delay, flipx_back, NULL));

I flip card using
id flipx = [CCFlipX3D actionWithDuration:1];
id flipx_back = [flipx reverse];
id delay = [CCDelayTime actionWithDuration:0];
[node runAction:[CCSequence actions: flipx, delay, flipx_back, nil]];
But, How can I show different image in back side of the node?

Related

Do a Command After an Action Has Been Completed - SpriteKit

In SpriteKit, I have a simple action for my sprite:
sprite.size = CGSizeMake(40, 10);
sprite.physicsBody =[SKPhysicsBody bodyWithRectangleOfSize:sprite.size];
sprite.position = CGPointMake(65, 230);
SKAction *changeImage = [SKAction setTexture:[SKTexture textureWithImageNamed:#"raniSlide"]];
SKAction *wait = [SKAction waitForDuration:.1];
SKAction *changeImage2 = [SKAction setTexture:[SKTexture textureWithImageNamed:#"raniStick"]];
SKAction *sequence=[SKAction sequence:#[changeImage, wait, changeImage2]];
[sprite runAction:sequence];
It changes textures, waits, then changes textures again. This is so it ducks for a certain time period. After ducking I want it to change back to its original size. When I put the code for changing size after the runAction command, it doesn't perform the first sprite.size = CGSizeMake(40, 10), and doesn't duck at all, as it stays the same size as it was in the beginning. So I was wondering how to make an if statement that when if an action has been completed, it then changes the size of the sprite.
You can't do it with an if statement, but take a look at runAction:completion:. You use it like:
[sprite runAction:sequence completion:^{
// the action is complete, change size back
}];

how to roll back(?) CCFadeout, CCScaleby and CCBlink? (cocos2d)

im trying to use CCFadeOut first for my game, which is the very first action
then I want to move onto scales and then blink without the effect of previous actions.
but when i change to the next action, the effect of previous action last..
for example, if i used fadeout before scale, then change to scale, the ccsprite is faded out with momented scale action
So, what i want to do is removing previous action's effect...
can you please give me some sample code for that?
Are you sequencing the actions one after another using CCSequence?
id fadeout = [CCFadeOut actionWithDuration:2];
id scale = [CCScaleTo actionWithDuration:2 scale:2];
id blink = [CCBlink actionWithDuration:2 blinks:5];
CCSequence* sequence = [CCSequence actions:fadeout, scale, blink, nil];
[sprite runAction:sequence];

CCRepeatForever Error

I've got a problem in my current game.
I'm trying to move a sprite based on the movement of a other physic body, for a map. This is my code:
...
NSMutableArray *mapObjetcs = [[[NSMutableArray alloc]init]autorelease];
[mapObjetcs addObject:swordman];
[mapObjetcs addObject:icon];
CCCallFuncND* iconMap = [CCCallFuncND actionWithTarget:self selector:#selector(mapLoc:mapObj:) data:mapObjetcs];
CCSequence* iconMapSequence = [CCSequence actions:[CCDelayTime actionWithDuration:1.0f/60.0f], iconMap, nil];;
CCRepeatForever* iconRef = [CCRepeatForever actionWithAction:iconMapSequence];
[self runAction:iconRef];
}
-(void) mapLoc:(ccTime)delta mapObj:(NSMutableArray*)mapObj
{
GB2Sprite *swordmanTemp = (GB2Sprite*)[mapObj objectAtIndex:0];
CCSprite *iconTemp = (CCSprite*)[mapObj objectAtIndex:1];
CGPoint swordmanPos = [swordmanTemp ccPosition];
float pos = (swordmanPos.x/convFactor)+65;
iconTemp.position = ccp(pos, 290);
}
Every time i run the code with the CCRepeatForever the games freezes, if i run the code without the CCRepeatForever the game run grat but dont refresh the icon in map.
Can anybody help me??? Thanks
Its a problem with running CCRepeatForever on layer itself.. Ofcourse it will freeze the game.. You can try for alternate solution I guess.. Instead of using a separate CCRepeatForever loop, use the update method of your layer.. As its already doing same thing that you want to do with your own action..
Another solution is make a same CCRepeatForever for your icon sprite.. and in its CCCallFuncND take the position of other object....
Hope this helps.. Try yourself.. If it doesn't work.. I'll try 2 give you code... Don't run CCRepeatForever Loop on your layer itself.. :)
To avoud such actions you can simply schedule some method with needed interval. smth like
[self schedule: #selector(methodToBeCalled) interval: intervalInSeconds];
just don't forget to unschedule it later

How to keep fps rate constant in cocos2d

I have 3 question.
How to keep fps rate constant(almost) in cocos2d.When lots of CCSprite created and removed within a small interval like (5-8) per second is it possible to keep frame rate lmost constant??
Is [self removeChild:sprite cleanup:YES] is enough or I should also use
CCTexture2D *texture = [sprite texture]; [[CCTextureCache sharedTextureCache] removeTexture: texture];
The following Part of code is responsible for my frame drop.How to accomplish same task in better way?
id fadeout = [CCFadeOut actionWithDuration:1.4f];
id call = [CCCallFunc actionWithTarget:self
selector:#selector(RemoveSmashedSprite:)];
CCSequence* sequence= [CCSequence
actions:fadeout, call, nil];
[smash runAction:sequence];
... and...
> -(void)RemoveSmashedSprite:(id)sender
{
CCSprite *sp = (CCSprite *)sender;
[self removeChild:sp cleanup:YES];
}
This is called 5-8 times per second. So the frame rate goes down. Can any one help me?
You shouldn't remove textures if you're going to reuse them in the short term. It would only benefit memory while having a great drawback in performance.
To maintain a constant frame rate you could try reusing the sprites instead of creating and removing them. Instead of calling removeChild you could set visible = NO and add it to an array of unused sprites. Then when you need a new of these sprites you check if there is any in that unused array and only create a new one if it's empty. This way you would minimize the amount of sprites created and destroyed.
Change this:
id call = [CCCallFunc actionWithTarget:self selector:#selector(RemoveSmashedSprite:)];
Into this:
id call = [CCCallFuncN actionWithTarget:self selector:#selector(RemoveSmashedSprite:)];
You can use remove action as your last action, instead of CCCallFunc.
id action = [CCActionRemove action];
This action will remove the node running this action from its parent

cocos2d hide/show sprites with animation

i am spending a lot for my time for a simple things i think.I want to hide and show a sprite
in scene.
myS = [CCSprite spriteWithFile:#"Background_Pause_pad.png"];
[myS setPosition:ccp(384,470)];
myS.opacity = 0;
[self addChild:myS z:1];
and when i need to appear it..
[myS runAction:[CCFadeIn actionWithDuration:1]];
and hide it
[myS runAction:[CCFadeOut actionWithDuration:1]];
but it does not work.....can anyone plz help??
Why do you use a Sequence for one action ?
You have to choose the animation you want !
E.g : if you choose CCFadeIn
[mySprite runAction:[CCFadeIn actionWithDuration:0.5f]];
I think you can try the below stuff of the code. It would work for you
id action1 = [CCFadeIn actionWithDuration:1];
id action2 = [CCDelayTime actionWithDuration:1];
id action3 = [CCFadeOut actionWithDuration:1];
[myS runAction:[CCSequence actions:action1,action2,action3,nil]];
As you need the fadein fadeout action it would generate it and display the Same.