Starting CCAction at particular time - iphone

I want to start InFinite CCAction at particular time. I tried using CCSequence but it supports only finite time animation.
Any idea?
Best Regards,
Paras

Put the action that you want to repeat inside of a method. Then put this in your init method
[[CCScheduler sharedScheduler] scheduleSelector:#selector(myMethod) forTarget:self interval:10 paused:NO];
This will call myMethod after 10 seconds, however once inside myMethod you'll want to unschedule it. So my method should look something like this.
- (void) myMethod
{
[[CCScheduler sharedScheduler] unscheduleSelector:#selector(myMethod) forTarget:self];
CCMoveBy *move = [CCMoveBy actionWithDuration:3 position:ccp(75,0)];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:move];
[mySprite runAction:repeat];
}

The last two lines are what you need.
CCMoveBy* move = [CCMoveBy actionWithDuration:3 position:ccp(75,0)];
CCCallFuncO* shot = [CCCallFuncO actionWithTarget:self selector:#selector(shoot:) object:enemy];
CCSequence* sequ = [CCSequence actions:move,shot,nil];
CCRepeatForever* repeat = [CCRepeatForever actionWithAction:sequ];
[sprite runAction:repeat]; //sprite here

Related

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

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

Is my class going to lose its memory

I have a class with two methods listed below. I call them from another class in a function. In this function I new up my class then call fireTorpedoContinued. In C# everything would be great, but I started to think about it. I'm not retaining the class anywhere and there seems to be nothing to stop it from going out of memory between fire torpedo and firetorpedo continued. Does anything hold onto this class or do I need to retain it? For example does the execute function class retain the class? What if that was not there would I be in trouble?
- (void) fireTorpedoContinued
{
[self.torpedoData.explosionSprite stopAllActions];
CCPlace *placeAction = [CCPlace actionWithPosition:_endPoint];
CCShow *showAction = [CCShow action];
CCCallFunc *callFunctionDeathCheck = [CCCallFunc actionWithTarget:self.deathCheckSelectorTarget selector:self.deathCheckSelector];
CCFadeOut *fadeOutAction = [CCFadeOut actionWithDuration:1.0f];
CCCallFunc *callfunctionAction = [CCCallFunc actionWithTarget:self.completedSelectorTarget selector:self.completedSelector];
CCSequence *sequenceAction = [CCSequence actions:placeAction, showAction, callFunctionDeathCheck, fadeOutAction, callfunctionAction, nil];
[self.torpedoData.explosionSprite runAction:sequenceAction];
}
- (void) fireTorpedo
{
[self.torpedoData.torpedoSprite stopAllActions];
CCPlace *placeAction = [CCPlace actionWithPosition:_startPoint];
CCShow *showAction = [CCShow action];
CCMoveTo *moving = [CCMoveTo actionWithDuration:2.0f position:_endPoint];
CCHide *hideAction = [CCHide action];
CCAction *callCompletedFunction = [CCCallFunc actionWithTarget:self selector:#selector(fireTorpedoContinued)];
CCSequence *sequenceAction = [CCSequence actions:placeAction, showAction, moving, hideAction, callCompletedFunction, nil];
[self.torpedoData.torpedoSprite runAction: sequenceAction];
}
Your question is a little fuzzy, but everything you need to know is explained here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#//apple_ref/doc/uid/20000043-BEHDEDDB

What is the best way to start Cocos2d Animation of Multiple Sprites?

I have an array randomAlphabets which contains CCSprite objects. I need to start animation on these objects. The randomAlphabets array (NSMutable) can contain max of 4 elements. I am running a loop and then starting the animation. Is this the correct way?
-(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) addAlphabetsOnScreen
{
for (int i=0; i<=randomAlphabets.count -1; i++) {
CCSprite *sprite = [randomAlphabets objectAtIndex:i];
[self generateRandomCoordinates];
sprite.position = ccp(x,y);
[self addChild:sprite];
[self startAnimation:sprite];
}
}
Sure, why not?
If have performance issues or sprites not starting their anims simultaneously, you might want to "prepare" the sequences for each sprite in one step (maybe after loading the level) and then just kick them all of in another step. 4 Sprites starting at the same time seems not too tough though.

iphone cocos2d CCSprite EXC_BAD_ACCESS

Friends i am new to cocos2d programming and Mac in general!
I have noticed this EXC_BAD_ACCESS errors ruining most of my time... Take the following snippet of code from the Geek & Dad's tutorial...
-(void) AddEnemyAtX:(int)x Y:(int)y {
CCSprite *enemy1 = [CCSprite spriteWithFile:#"enemy1.png"];
enemy1.position = ccp(x,y);
[self addChild:enemy1];
[self animateEnemy:enemy1];
NSLog(#"%#", enemy1);
}
-(void) animateEnemy:(CCSprite *)enemy {
ccTime actualDuration = .5;
id actionMove = [CCMoveBy actionWithDuration:actualDuration
position:ccpMult(ccpNormalize(ccpSub(_player.position,enemy.position)), 10)];
id actionFinished = [CCCallFunc actionWithTarget:self
selector:#selector(animateEnemyFinished:)];
[enemy runAction:[CCSequence actions:actionMove,actionFinished,nil]];
}
-(void) animateEnemyFinished:(id)sender {
CCSprite *enemy = (CCSprite *)sender;
[self animateEnemy:enemy];
}
here _player is a global variable and accessible everywhere, I call AddEnemyAtX: Y: and pass some coordinates. My problem is the first time the loop runs fine... But again when the control is passed from animateEnemyFinished to animateEnemy then the app crashes mentioning "EXC_BAD_ACCESS"!
From what i figured out, the Sprite reference is not passed correctly! Help!
CCSprite *enemy1 = [CCSprite spriteWithFile:#"enemy1.png"];
gives you an autoreleased object. This means you should not call
[enemy1 release]
and 2nd after you set
enemy1=nil
you can't do
[self animateEnemy:enemy1];
because you give nil to animateEnemy:
Removing
[enemy1 release];
and
enemy1 = nil;
from your code should fix your problem.
Wow!!
Atlast figured it out...
A small mistake... just replacing the line
id actionFinished = [CCCallFunc actionWithTarget:self
selector:#selector(animateEnemyFinished:)];
with
id actionFinished = [CCCallFuncN actionWithTarget:self
selector:#selector(animateEnemyFinished:)];
did the trick! What i understood from this was that #selector() passes the id of the object which called upon it but at the same time when we use it along with CCCallFuncN it passes the id of the Node just parent to the object which called upon it!