Do a Command After an Action Has Been Completed - SpriteKit - if-statement

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

Related

CCAction in box2d world

i'm newbie to both cocos2d and box2d and i've been struggling for two days with this problem : i have a scene with many sprites dropping down (with bodies attached to them). and i have a BackgroundLayer from which i add my background image into the scene (which is not involded into the physics simulation). In my backgroundLayer i'm trying to perform an action on a sprite :
(it blink in the first position and jump directly to the end position )
id flyBubble = [CCEaseInOut actionWithAction:[CCMoveTo actionWithDuration:0.7 position:randomEndPosition]];
but my sprite doesn't respond at all to this action!! my sprite doesn't have any b2body attached and seems like it respond to the tick: method of the physics world (which is in my Main Scene). How can i perform an action to a sprite that doesn't have a b2body attached.
any help would be appreciated!!! thanks
here is the entire code :
CCSprite *bubble = [CCSprite spriteWithFile:#"bubble.png"];
[self addChild:bubble];
CGPoint startPosition = ccp(100, 100);
bubble.position = startPosition;
CGPoint endPosition = ccp(400, 400);
id flyBubble = [CCEaseInOut actionWithAction:[CCMoveTo actionWithDuration:0.7 position:randomEndPosition]];
id remove = [CCCallBlockN actionWithBlock:^(CCNode *node) {
[self removeFruit:(CCSprite *)node];
}];
[bubble runAction:[CCSequence actions:flyBubble, remove, nil]];
I guess source and destination position of sprite is same. So no difference in action.
try like this..
sprite.position = ccp(0,0);
id flyBubble = [CCMoveTo actionWithDuration:0.7 position:randomEndPosition];
[sprite runAction:flyBubble];
i did a little mistake that costs me lot of times. it was in this line of code
id flyBubble = [CCEaseInOut actionWithAction:[CCMoveTo actionWithDuration:0.7 position:randomEndPosition]];
CCEaseInOut doesn't work i don't know why!! when i used CCEaseExponentialInOut it worked perfectly.
Thanks anyway!

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.