Showing a frame when collision is detected (cocos2d iPhone) - iphone

I have a hero character all set up with a CCSpriteBatchNode, which has all the animation and frames. But I wonder, how do I display a frame when the hero is hit (I set up collision detection already). How do I make this happen? Do I put it inside the CCSpriteBatchNode? It's not part of moving, only for special occasions.

You can set your hero invisible ( [sprite setVisible: NO]) and show another sprite at the hero's position.
More then it, you can keep this sprite in your hero class object and provide a method which will change the visible sprite when collision happens. Something like this
-(void) onCollision
{
[heroMainSprite setVisible:NO];
[heroCollisionSprite setVisible:YES];
}
Such solution will also preserve all your logic, that has been done before.

When hero gets hit.. Change the texture..
[spr setTextureRect:CGRectMake(startX, startY, width, height)];
It needs to be in the same batch node as the rest of the animation..

Related

How can I synchronize both movement and an animation using cocos2d?

I need help getting a CCSprite to animate and move in a synchronized fashion repeatedly. For example I need to move the sprite from one game board space to the next while walking. While I can call each action right after one another, I have found that when run in rapid succession the two don't fire at the same time causing either an additional animation cycle to run between moves or movements to not be aligned with the start/end of the animation.
Here are examples of how I'm creating the animation and movement actions.
CCAnimation *animRunUR_ = [CCAnimation animationWithFrame:#"actor_run_ur" frameCount:8 delay:runAnimDuration_];
CCAction *_actionAnimRunUR = [[CCSequence actions:
[CCAnimate actionWithAnimation:animRunUR_],
[CCCallFunc actionWithTarget:self selector:#selector(animationFinished)],
nil] retain];
CCAction *_action = [[CCSequence actions:
[CCMoveTo actionWithDuration:duration_ position:gridbox_.center],
[CCCallFunc actionWithTarget:self selector:#selector(nextMovement)],
nil] retain];
Each of the above actions are called directly on the CCSprite as follows.
[self.sprite runAction:_action];
[self.sprite runAction:_actionAnimRunUR];
The duration of the animation is calculated to be equal to the amount of time it takes to move to the next game board space. In addition, these are fairly straight forward actions but I'm really having a hard time keeping them synced as they continually repeat.
Any best practices or suggestions would be greatly appreciated as I'm sure this is a common issue in game development with sprites that continually animate while moving. Then trying to keep smooth transitions between things like walk, run, and jump without split second pauses between actions. Thanks for your feedback and suggestions.

scale the background layer touch position is changed?

I am doing some r &d in cocos2d.I have one backgroundLayer and add one sprite in that layer.
In touches Moved i changed the sprite position to current touch position.If background layer is not scaling that means backgroundLayer scale is 1 ,the code is run perfectly (simply set the touch position to sprite position).if i scaling the backgroundLayer i have the problem sprite doesn't move touch position ..please any one help me.
Here is my code:
layer1=[CCLayer node];
[self addChild:layer1];
layer1.scale=2;
iconImg=[CCSprite spriteWithFile:#"Icon-72.png"];
iconImg.position=ccp(512,384);
[layer1 addChild:iconImg];
In Touches Moved
iconImg.position=tchLocation;
My problem is after scaling the backgroundLayer how to find the touch position according to the background layer position..any one guide me...
How do you calculate your tchLocation?
It should work with:
CGPoint *tchLocation = [layer1 convertTouchToNodeSpace:touch];
Where touch is your UITouch.

How to add transition effect in Current running scene in cocos2d iphone

How to add transition effect in Current running scene in cocos2d iphone. Means I am making a game and after each goal I want to give a fade effect or any type of effect on the current running scene.
If I write this, It replace current scene to its new scene. But I don't want to replace scene.
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0f scene:[GamePage scene]]];
Is there some way to show effect on the current page like this. I know that it is wrong but I want something like this :
[self transitionEffect:[CCTransitionFade actionWithDuration:0.5]];
For Scene,Layer (Subclass of CCNode) action related to Opacity will not work. !
You can Either Use transition, or Must have to apply CCFadeTo to all of your sprite.
But If you are choosing to CCFadeTo to all sprites, this will require allocation of lots of actions suddenly ! FPS slow down !!
Another best approach:
Tell to your designer, to make 1 x 1 pixel Square black dot image.
Add this code at last in the init method.
CCSprite *temp=[CCSprite spriteWithFile:#"squaredotBlack.png"];
temp.position=ccp(s.w/2,s.h/2);
[self addChild:temp z:50000]; //set as most top layer
temp.scaleX=s.w;
temp.scaleY=s.h;
temp.opacity=0;
Then Apply, For "Fade out" process of whole screen, Increase the opacity.
temp.opacity=0;
[temp runAction:[CCFadeTo actionWithDuration:1 opacity:255]]; //0 to 255
Then Apply, For "Fade In" process of whole screen, decrease the opacity.
temp.opacity=255; // this will cover whole screen with black color
[temp runAction:[CCFadeTo actionWithDuration:1 opacity:0]]; //255 to 0
you can run an action on the entire CCLayer
[self runAction:[CCFadeOut actionWithDuration:0.5f]];
or you can use CCFadeTo to fade to a desired opacity.

UIImageview won't update after changing the center

I am trying to create a small augmented reality application where I move an image on top of the camera capture. So the only thing I change is the center of the UIImageview:
[imageView1 setCenter:CGPointMake(x-16, 240)];
and the center gets updated but the position of the image on the screen stays the same.
after the center update, this gets called:
[self.imageView1 performSelectorOnMainThread:#selector(setImage:) withObject:testImage waitUntilDone:YES];
The funny thing is that in the first iteration it actually updates the position. But only the first time.
Any ideas?
Try:
imageView1.center = CGPointMake(imageView1.center.x-16, 240);
The accepted answer in this post (UIImageView not responding to centering) suggests that the problem is autolayout, which makes sense.
This post (Can I disable autolayout for a specific subview at runtime?) explains how to disable autolayout for specific elements.
Another answer suggest you add constraints (for x and y), create IBOutlets for these constrataints and update those to move the UI-element.

Cocos2d adding a background image to a layer?

I just finished reading some of the cocos2d documentation, and as far as I understand to add a background image to a layer you have to do something like:
Sprite * bg = [Sprite spriteWithFile:#"Background.png"];
[layer addChild:bg z:0];
Allthough as far as my testing goes you can just directly add the sprite to the scene like this:
#implementation IntroScene
- (id) init {
self = [super init];
if (self != nil) {
Sprite * bg = [Sprite spriteWithFile:#"Background.png"];
[bg setPosition:ccp(240, 160)];
[self addChild:bg z:0];
[self addChild:[MenuLayer node] z:1];
}
return self;
}
What is the difference between this 2 options, and what is the better way to set a background for a layer?. Any help will be greatly appreciated.
You do not need to have a layer, but they are handy. Think of the layers as a way of grouping sprites (both visually and organizationally). You may want your background images grouped as a layer and all the enemies in a game as another layer. This allows you to deal with each set more cleanly. e.g. You can remove a specific layer instead of finding all of the enemies and removing them.
If it helps, play with layers in Photoshop or a graphics editing program. Layers in cocos2d work similarly.
For a simple scene, adding the background to a layer or to a scene won't make much difference. But it doesn't take long before the benefits of layering kicks in.
Also layers control drawing order (over and above any inter-layer z ordering).
In geoSpark, for example, I have a background layer, a spark layer, a UI layer, etc. (Simplified). But this allows me to make sure my UI stuff is always above any game stuff (the spark layer) and all that is above the background.
If your game does only additive rendering, draw order won't matter much. :)
-- David
scene is a blackboard, layer is a page... news is wrote on page, but, if you want you can write on the board. If you want erase all new.
In cocos2d we can add any node on any node. But it depends on level of hierarchy.
You can add Layers on Scenes. If you want same property or look for all layers that you add on a CCScene you can add those in CCScene (as in your second case). but if you want to have different feature on each layer add those to CCLayer.
CCLayer is meant for grouping a set of nodes/sprites for some purpose. For example you can add a layer which includes options after pausing a game, or HUD layer for displaying som kind always on screen along with some scrolling layer (liken in some scrolling platform games).
Finally you can even CCNode on a CCScene, a layer on another layer, and even a CCNode on a CCSprite.
But the order of drawing in graphics context in cocos2d differs.
Hope you got the difference.
Good luck!