Cocos2d CCFollow when there are multiple layers - iphone

Essentially, when I have one CCLayer for background and platform sprites and another for player sprite and HUD, how should I go about calling CCFollow if I want my camera to always center at the player with the HUD in the same layer moving accordingly as well?
Right now the background layer is a subclass of CCLayer that has a CCSpriteBatchNode for drawing objects in the environment. It contains an instance variable CCLayer *HudLayer that has a CCMenu and another CCSpriteBatchNode for the player animation sprites. They are both direct children of a CCScene, meaning HudLayer is not a children of BackgroundLayer. I'm now calling the CCFollow as [self runAction:[CCFollow actionWithTarget:playerSprite worldBoundary:worldRect]] where self is the BackgroundLayer.
The camera follows the box2d physics body of the player correctly, and the menu moves with it too, but whenever the camera moves, let's say to the right, the player sprite moves twice as fast to the right, leaving its physics body behind. So after moving to the right for a bit the playerSprite is outside of screen. playerSprite.pos and playerSprite.position seem to have nothing wrong though.
I've tried calling the CCFollow in different places but either the HUD refuses to move or the camera won't move or the player moves weirdly. What's wrong with it?

I FIGURED IT OUT! The key here is to put the player sprite in some layer other than the HUD layer. I added my CCSpriteBatchNode for the player sprites as a child of backgroundLayer instead of the HudLayer without changing anything else and it worked.
So basically everything that needs to always stay on screen as the HUD should be in one layer, while the target node for CCFollow should be in a separate layer (in the background in my case). The caller layer of CCFollow should contain the target node as a direct child. This tutorial helped me a great deal: http://www.raywenderlich.com/4666/how-to-create-a-hud-layer-with-cocos2d

Related

What's the equivalent of convertToNodeSpace in Sprite Kit

Let me first introduce you to the node hierarchy:
SKView->SKScene->SKNode
I've added a UIPinchGestureRecognizer to the view so that I can zoom in and out my content. Here's the code in SKScene object:
-(void)handlePinch:(UIPinchGestureRecognizer*)pinchRecognizer{
[self runAction:[SKAction scaleBy:pinchRecognizer.scale duration:0]];
pinchRecognizer.scale = 1;
}
Everything's OK except for two things:
In my node's touch events I check the position of the touch and act accordingly. Everything works fine as long as the scale of the scene is untouched. But if I zoom in or out I can no more do this. It's because even if I seem to touch the same point on the screen it's actually different before and after zooming. In Cocos2D I had exactly the same problem. But I could solve this problem by just converting the touch point to node space with convertToNodeSpace method. There should be an equivalent in Sprite Kit. What is it?
When pinching, anchor point is the lower left corner. Is there a way to make it zoom from where the pinching occurs?

Control Sprite in another scene from HUD layer

I have a sprite in scene1.m and I have a button in HUD layer hud.m. Now a button appears in Scene1.
How can I get control of scene1 sprite from HUD layer. Any ideas?
With cocos2d, you can assign a tag to your sprite (or button, or any class derived from CCNode), and to each of its hierarchy of parents (see CCNode class, setTag() ). You can then use these tags to retrieve the corresponding nodes from anywhere in your code, with CCNode getChildByTag().

how to detect touch which appear on the location greater than resolution of device

I'm looking for help. I'm trying to make some puzzle game and I have thin scrollable layer at the bottom of my main layer which contains some puzzle shapes. I can scroll the layer and see every shape but shapes were positioned manually in code on the scrollable layer and the problem is, if I try to detect if I touched on sprite, which position is greater than 1024(in first iPad) it doesn't work. it doesn't work because touch can have position inside 1024X768 and the position of shape is for example 1500x100. to make it clearer, shapes are sprites and i try to detect them using CGRectContainsPoints method. Is there any other way to make it or have you any ideas? thanks in advance :]
What you could do is subclass your sprite, create a delegate for it and assign your main view/class as the delegate.
Implement the appropriate touch method, and send the message of what sprite was selected to your delegate (Main view or desired controller class).
With this every sprite has the same delegate, and sends a message to your controlling class as to what sprite has been selected and continue with desired functionality. No need for CGRectContainsPoint method.
This is cleaner, and more efficient.
Hope this helps!

Cocos2D iPhone - adding a CCLayer on top of another

I am newbie to Cocos2d. I have this layer that represents my main game scene. Lets talk in terms of Cocos2D default template. In this case, my main game scene would be HelloWorldLayer.
Now I want to present a menu. I have create the menu as an individual subclass of CClayer.
How do I make the menu appear using some kind of transition on top of the main scene?
If I use something like
CCScene *menu = [Menu scene];
[[CCDirector sharedDirector] replaceScene:
[CCTransitionCrossFade transitionWithDuration:0.5f scene:menu]];
I would be using the menu as a scene and replacing the main scene with it. This is not what I want. I want to make the menu appear on top of the main scene, using some kind of transition and if possible fading the main scene to 50% or whatever.
How do I do that?
thanks.
Instead of replacing the scene, simply make your Menu class a CCLayer and add it existing scene. You can set opacity and add masking sprites as needed to fade out the bottom layer if you need to.
CCLayer* newLayer = [Menu layer];
[self addChild: newLayer];
You will need to handle much of the details on which layer captures input, but this is the basic idea.

out of the scene elements overlap the previous scene during transitions

I am developing a cocos2d based IPhone game where a scene replace another with a Left to Right Transition. The new scene has a background a bit bigger than the scene. That is just because I move it according to the device tilt to create a sort of 3D effect.
Unfortunately during the transition of the scene I can see the background outside the scene overlapping the previous one :(
I have then tried to use the z-order in such a way the previous scene should go on top of the new one bug it does not work the way it should.
do you have any suggestion?
Thank you in advance
I think you need do crop your scenes layer by overriding it's -(void)visit method.
That's how I did it:
- (void) visit {
glPushMatrix();
glEnable(GL_SCISSOR_TEST);
glScissor(cropRect.origin.x,
cropRect.origin.y,
cropRect.size.width,
cropRect.size.height);
[super visit];
glDisable(GL_SCISSOR_TEST);
glPopMatrix();
}
May be you'll need to play a bit with cropRect origin and size values.
Also you may need to add some device orientation changing support