How to stop runAction in cocos2d - iphone

How to stop all actions in game after some time?
in below code i am calling runaction on a sprite.
id actionMove = [CCMoveTo actionWithDuration:actualDuration*2.5 position:ccp(winSize.width + (target.contentSize.width/2), actualY)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:#selector(spriteMoveFinished:)];
id sequece = [CCSequence actions:delayTime1, calFun1, delayTime2, calFun2,actionMove, actionMoveDone, nil];
id repeate = [CCRepeatForever actionWithAction:sequece];
[target runAction:repeate];
In this code a sprite is continuously moving. how to stop the above action after some time or after some condition ?

In runAction call Function and stop action for sprite.
[CCCallFuncN actionWithTarget:self selector:#selector(setInvisible:)];
- (void)setInvisiblestone:(CCNode *)node
{
[target stopAllActions];
}

All action stop in method.
-(void)gameover
{
[self unscheduleAllSelectors];
}

Related

getting sender object in cocos2d

I am making a game using cocos2d. By using the following code,I have added an animation.How do I send the CCSprite reference?
if(sprite != monkey)
{
[self scheduleOnce:#selector(animate_sprite:) delay:0.1f];
}
-(void)animate_sprite:(ccTime) dt
{
id s2 = [CCScaleTo actionWithDuration:0.5 scaleX:2.0 scaleY:2.0];
id fun = [CCCallFuncN actionWithTarget:self selector:#selector(spriteDone:)];
[sprite runAction:[CCSequence actions:s2,fun,nil]];
}
How to get the sprite reference in animate_sprite method?
You can use performSelector:withObject:afterDelay this will do the same thing.
if(sprite != monkey)
{
[self performSelector:#selector(animate_sprite:) withObject:sprite afterDelay:0.1f];
}
-(void)animate_sprite:(CCSprite *)sprite
{
id s2 = [CCScaleTo actionWithDuration:0.5 scaleX:2.0 scaleY:2.0];
id fun = [CCCallFuncN actionWithTarget:self selector:#selector(spriteDone:)];
[sprite runAction:[CCSequence actions:s2,fun,nil]];
}
So just edit your method to use the sprite and not the ccTime object since you are not using it at all.

Cocos2d Action only being run once

I am trying to run an animation on a sprite each time a button is pressed.
But for some reason, the action is only ran once and wont run again like i need it to.
Here is my action if it helps.My action is declared as an id instance variable.
moveUp = [CCMoveTo actionWithDuration:3 position:ccp(60,self.position.y+200)];
Are you trying to re-use the same action? Like so:
moveUp = [CCMoveTo actionWithDuration:3 position:ccp(60,self.position.y+200)];
[self runAction:moveUp];
// some time later …
[self runAction:moveUp];
Then this won't work. You have to create a new action every time, like so:
CCAction* moveUp = [CCMoveTo actionWithDuration:3 position:ccp(60,self.position.y+200)];
[self runAction:moveUp];
// some time later …
CCAction* moveUp = [CCMoveTo actionWithDuration:3 position:ccp(60,self.position.y+200)];
[self runAction:moveUp];

how to make a jerking in a sprite motion

all, I am making a game in which a sprite is move from one position to another through a jerk of 19 pixel, basically I want to make a game just like stacker online. in the app store, in this game the box is move through a jerk of 19 piexel , and the object is moving is in the grid , i have following code but I not give me the jerk motion
-(void)actionLayer
{
isRunning = 0;
addPixedlValue += 19;
id actionMove = [CCMoveTo actionWithDuration:6 position:ccp( 50,109)]; //300,100
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:#selector(LayerAction:)];
animateAction = [CCSequence actions:actionMove, actionMoveDone, nil];
NSLog(#"pixel value %d",addPixedlValue);
[boxSprite runAction:animateAction];
}
-(void)LayerAction:(id)sender
{
addPixedlValue +=19;
id actionMove = [CCMoveTo actionWithDuration:6 position:ccp( 270,109)]; //300,100
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:#selector(actionLayer)];
animateAction = [CCSequence actions:actionMove, actionMoveDone, nil];
[boxSprite runAction:animateAction];
}
I think your problem can be sort out By scheduling a selector for a time interval say 0.1 and you can update the position according to your self.
-(void)moveSprite
{
sprite.position = ccp ( sprite.position.x + 5 , sprite.position.y);
}
And unshedule the selector whenever needed to stop the sprite.
For Scheduling a selector
[self schedule : #selector ( moveSprite ) interval : 0.1];
For unscheduling
[self unschedule : #selector ( moveSprite )];
This help you in make jerking movement.

iphone cocos2d CCSequence of Action and CCParticleSystem animation

I´m new to Cocos2d. I´m trying to run two animations one after another.
The first one is:
CCAction *walkAction;
CCAnimation *walkAnim = [CCAnimation
animationWithFrames:walkAnimFrames delay:0.15f];
bear = [CCSprite spriteWithSpriteFrameName:#"normal1.png"];
walkAction = [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO];
[bear runAction:walkAction];
[spriteSheet addChild:bear];
The second which I want to fire right after the first one is:
CCParticleSystem *killPigAnim = [CCParticleSystemPoint particleWithFile:#"killPigAnim.plist"];
[self addChild:killPigAnim];
How can I achive that when the second one is not an action but the CCParticleSystem object.
You can use the action CCCallFunc to either call the start method on the particle system or call a method in your class which starts the particle system.
i.e.
-(void) startParticles
{
//Start your particles
}
-(void) myOtherMethod
{
...
walkAction = [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO];
CCCallFunc *callAction = [CCCallFunc actionWithTarget:self selector:#selector(startParticles)];
[bear runAction:[CCSequence actionWithActions:walkAction, callAction, nil];
...
}

CCSequence Firing Actions in Cocos2d

I have three actions that are triggered in the CCSequence. The way I want it to be fired is that first the sprite should move in the center of the screen and then the scale action is fired. But for some reason the sprite moves to the center of the screen correctly but when the scale is fired it uses the old sprite position.
id actionRotate = [CCRotateBy actionWithDuration:0.6 angle:360];
id disappear = [CCFadeTo actionWithDuration:.5 opacity:0];
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:#selector(removeAlphabetToFindFromView:)];
id actionScale = [CCScaleBy actionWithDuration:0.6 scaleX:10 scaleY:10];
id moveTo = [CCMoveTo actionWithDuration:0.6 position:ccp(windowSize.width/2, windowSize.height/2)];
//[self removeAlphabetToFindFromView2:alphabetToFind];
[alphabetToFind runAction:[CCSequence actions:moveTo,actionScale,disappear,actionMoveDone, nil]];
UPDATE 1:
Maybe the startAnimation method has something to do with this. I have public x and y variables which is used as x and y positions for 4 different sprites:
-(void) startAnimation:(CCSprite *) sprite
{
[self generateRandomCoordinates];
id actionMove = [CCMoveTo actionWithDuration:3.0 position:ccp(x,y)];
id actionRotate = [CCRotateBy actionWithDuration:0.0 angle:rotateBy];
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:#selector(finishedMoving:)];
[sprite runAction:[CCSequence actions:actionMove,actionRotate, actionMoveDone, nil]];
}
-(void) finishedMoving:(id) sender
{
if(counter == randomAlphabets.count)
{
counter = 0;
}
CCSprite *sprite = [randomAlphabets objectAtIndex:counter];
[self generateRandomCoordinates];
[self startAnimation:sprite];
counter +=1;
}
UPDATE 2:
As expected the x and y used in the startAnimation method (getRandomCoordinates) were causing the problem. So, I removed all the actions before firing the sequence and now it works fine.