need help in understanding CCMoveTo? - cocos2d-iphone

I have downloaded the sample demo and trying to understand it.but Stuck on following code
id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(-target.contentSize.width/2, actualY)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:#selector(spriteMoveFinished:)];
[target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
How CCMoveTo moves object from one location to another as this object moves from left to right.

How CCMoveTo moves object from one location to another as this object moves from left to right.
not like left to right?
If the object(sprites) anywhere and you are willing to moveto some position from current position
example
current sprite position
(100,100)
then if you use CCMoveTo with (200,200) then it will move to (200,200)
then if you use CCMoveBy with (200,200) then it will move to
currentposition+neededposition
so (100+200,100,200) which implies (300,300);

Related

How to animate something in the the VOID statement? XCODE

-(void) redafter1
{
red = [CCMenuItemImage
itemFromNormalImage:#"red.png" selectedImage:#"redclick.png"];
red.position = ccp(175, 725);
redMenu = [CCMenu menuWithItems:red, nil];
redMenu.position = CGPointZero;
redMenu.scale = .75;
[self addChild:redMenu z:10];
}
How would I go about animating this object to move to another location on the screen? I am very new to this, please be basic in your explanation.
If you want to animate CCNode (this is base class of all cocos2d objects such as layers, sprites, labels, menuitems, etc.), you have to use actions mechanism. To move object, use CCMoveTo, CCMoveBy actions with runAction: method.
id moveAction = [CCMoveTo actionWithDuration: animationDuration position: targetPosition];
[nodeToAnimate runAction: moveAction];
In your case if you will run action on the object right after adding it to the some parent, that is visible (your scene or other visible parent), action will be started right after object appear.
You add [redMenu runAction:[CCMoveTo actionWithDuration:time position:place]]; (you choose time and position) at the end of your redafter1 function so when the parent method is eventually called by a method such as init your menu will move.Remember you cannot move a CCMenuItemImage because it's locked to the CCMenu's position. You have to move the CCMenu itself.Hope this was helpful.

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

stack of actions for one sparite in cocos2d

I've got a question: My games hero can jump (CCJumpTo) and move to forward and backward direction (CCMoveTo).
I faced with problem: if game recived a command from user to move the hero, but in this time hero is jumping (CCJumpTo), how can I do this command after the end of jumping. (can I use some stack of actions for one sparite in cocos2d)
I can't use immediately [self.sprite stopAllActions]; to begin CCMoveTo because the hero have to finish the jump action and if I use CCMoveTo the action CCJumpTo will not finish. Hero can't jump and start to move to forward and backward at height.
sorry for my English
For example, you can store your stack of actions in array, then run action like this
- (void) runNextAction
{
id actionFromStack = [stack objectAtIndex: 0];
[stack removeObjectAtIndex: 0];
id callback = [CCCallFunc actionWithTarget: self selector: #selector(runNextAction)];
id sequence = [CCSequence actionOne: actionFromStack two: callback];
[self runAction: sequence];
}

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