I'm using CCSequence Actions and I want my sprite to hold at a specific action while touch and hold and continue the sequence once the touch is ended.
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
id downward = [CCRotateTo actionWithDuration:0.1 angle:45];
id straight = [CCRotateTo actionWithDuration:0.1 angle:0];
id stable = [CCSequence actionWithDuration:1.0];
id upward = [CCRotateTo actionWithDuration:0.1 angle:-45];
id Seq = [CCSequence actions:downward, straight, stable, upward, nil];
[mySprite runAction:Seq];
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
}
I want this "id stable = [CCSequence actionWithDuration:1.0];" replaced with a float of time interval while the Touch is on Hold. And reset it when the touch ends.
Any help is much appreciated.
If i recall well you should be able to set a tag to your action (setTag).
You could set the tag in ccTouchBegan and retrieve this particular action from its node using this tag in ccTouchEnded, which would allow you to pause, terminate, or replace the action as needed.
Related
i have 2 layers and on ccTouchMoves event i have to destroy and recreate sprite to move from 1st layer to 2nd
i did this something like that
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
sprite = [CCSprite spriteWithFile:#"file.png"];
[[self parent] addChild: sprite]
if (sprite)
{
[sprite ccTouchBegan:touch withEvent:event];
// [character ccTouchMoved:touch withEvent:event];
}
[self removeFromParentAndCleanup:true];
}
sprite created and called method ccTouchBegan but after that method everything is terminate
how to call ccTouchMoved and ccTouchEnd just like simple touch event
If it's the same sprite, why destroy and recreate it? You can just keep on using the same sprite. In Kobold2D I added this method in a CCNode category to transfer ownership of a node from its current parent to a different parent:
-(void) transferToNode:(CCNode*)targetNode
{
CCNode* selfNode = [self retain];
[self removeFromParentAndCleanup:NO];
[targetNode addChild:selfNode z:selfNode.zOrder tag:selfNode.tag];
[selfNode release];
}
The important part is to remove the node (your sprite) from its current parent without cleaning up, so that schedulers and actions keep running. Then just add it as child to a different node (your 2nd layer).
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];
So I have a selector that need to be perform after certain amount of delay / interval, and I still can't find the solution. Put CCDelayTime before the CCCallFunc in CCSequence doesn't work. But it's working at simply put something like schedule:interval: or performselector:afterdelay.
if (shootDelay > kBlastBombLaunchingDelay) {
[self createFireProgressBar];
action = [CCSequence actions:
[CCDelayTime actionWithDuration: kBlastBombLaunchingDelay],
[CCCallFunc actionWithTarget:self selector:#selector(launchBomb)],
nil];
//[self performSelector:#selector(launchBomb) withObject:nil afterDelay:kBlastBombLaunchingDelay];
//[self schedule:#selector(launchBomb) interval:kBlastBombLaunchingDelay];
shootDelay = 0;
}
[self runAction:action];
Somehow, the selector launchBomb is not called. Sorry for the silly question.
You don't seem to have called:
[self runAction:action];
User found own solution
I think I've found the solution. Just like I said earlier, it's a silly question, the error was because of my messy algorithm. Put a lower value than kBlastBombLaunchingDelay for the CCDelayTime and change the if position to the statement which change my hero state. And it works now.
I have a CCSprite called sprite that is a child of a CCLayer called movingLayer that is itself a child of the current CCLayer running my game logic, so it is self in this case. movingLayer is moving back and forth across the screen in a repeat forever action and sprite is riding along on it. I want to get sprite to "get off" of movingLayer and "get on" to self where it can do some actions of its own.
When I try to do this, I need to tell movingLayer to remove sprite and self to add sprite. This is what I did...
- (void)attack:(id)sender
{
[sprite stopAllActions];
[movingLayer removeChild:sprite cleanup:YES];
[self addChild:sprite z:1];
CGFloat distance = ccpDistance(sprite.position, ccp(sprite.position.x, -sprite.contentSize.height/2));
id goDown = [CCMoveTo actionWithDuration:distance/moveDuration position:ccp(sprite.position.x, -sprite.contentSize.height/2)];
id repeatDown = [CCRepeatForever actionWithAction:[CCSequence actions:[CCMoveTo actionWithDuration:0 position:ccp(sprite.position.x, sprite.contentSize.height/2+self.contentSize.height)], [CCMoveTo actionWithDuration:moveDuration position:ccp(sprite.position.x, -sprite.contentSize.height/2)], nil]];
id attackAction = [CCSequence actions:goDown, repeatDown, nil];
[sprite runAction:attackAction];
}
I think stopAllActions is redundant, but that's not the problem. If I remove sprite from movingLayer before adding it to self I crash for accessing a zombie, and if I try to add it to self first, I crash for trying to add something already added.
Have you tried setting cleanup to NO?
Alternatively, try to retain sprite before you remove it from movingLayer, then release it when you're done with it:
[sprite retain];
[movingLayer removeChild:sprite cleanup:YES];
[self addChild:sprite z:1];
[sprite release];
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