SubViews with Cocos2d - iphone

I have a helloworld scene that I would like to add a subview to.
How is this done in cocos2d? I want to add a subview near the lower part of screen.
I want this view to be controlled by the helloworld scene, hiding and showing the subview as needed.
How is this done in cocos2d?
Thanks

Add the subview (probably CCLayer) as a child to the hello world scene.
CCLayer* subview = //initialize
[subview setPosition:ccp(X,Y)];
[self addChild:subview];

You can add a CCLayer as a child to the CCScene or to the main CCLayer inside of it.

Related

Moving the UIImageView Place in UIScrollview to Another UIImageView Placed on Next UIScrollview

I am new to iPhone development. I have added lot of UIImageview to the UIScrollview. While clicking that UIImageView I need to move the particular UIImageView to another UIscrollview placed on the bottom of the screen as UIImageView.
You can use a PanGestureRecognizer for this. You need to move the view to the superview of the scrollView while dragging and then after releasing it, you need to place it in the new scrollView correctly.
I wrote a library for exactly this, check out my JDDroppableView Library: https://github.com/jaydee3/JDDroppableView

Bring bottom View to Top View while touchesMoved

I have a grid of views, which are being added as subviews to a main view. When I drag a view which was added prior to the next one, and try to drag it underlays the view rather than overlay that view. How can I fix that content while the view are being dragged on touchesMoved?
[self.view bringSubviewToFront:yourView];
Hope this helps...
There is also another way, You can import
#import <QuartzCore/QuartzCore.h>
and each of view set zPosition like this
view.layer.zPosition = z;

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.

How to move UIView outside its SuperView?

So, I have a custom UIView inside a UIScrollView. I am able to detect the touches Events in the customUIView. I am trying to drag the UIView outside the UIScrollView onto a Canvas (UIView). However, when it gets out of bounds from the SrollView, it just hides behind it? How can I overcome this? Thanks guys!
When you start dragging the view, remove it from the superview that is the UIScrollView, and make it a subview of the app's Window, and bring it to front (z-order-wise).
But first you need to calculate (convert) the dragging view's frame with regards to the window, since the coordinates will be different after changing its superview.
you can use this method [UIView removeFromSuperView];

Use CCLabelBMFont inside UIViewController Inside Node

I'am doing a game, when the player lose, and screen of Game Over appears. But my Game Over screen its in a UIViewController. I call it in my Director, and alls right.
The Game over screen appears like overlay in the game
But i am using Custom Fonts, and in my Game Over screen I want to use it. But how could I use only "CCLabelBMFont" in the Game Over.
I think a way (no the better one). Its that you print in a bigger z-index than the Game Over the text I want it with "CCLabelBMFont". But I want to have all the stuff of Game Over in the GameOver.m
What could I do? Thanks to everybody.
To present anything from Cocos2D on top of a UIKit view, you'll have to make the Cocos2D view transparent and introduce a dummy view. You can't achieve this effect with the z order because that affects only the nodes in Cocos2D's OpenGL view.
To make the Cocos2D view transparent you need to change the color depth to 32 bit (RGBA8888) from the default 16-Bit mode in the EAGLView initialization (app delegate). Then setup the view hierarchy with a dummy view, so that you can actually add UIKit views in the background and the Cocos2D view as the foremost view.
The basic approach is to do this in your app delegate didFinishLaunching method:
UIView* dummyView = [[UIView alloc] initWithFrame:[window bounds]];
[dummyView autorelease];
[dummyView addSubview:[CCDirector sharedDirector].openGLView];
rootViewController.view = dummyView;
[window addSubview:rootViewController.view];
// make the cocos2d view transparent:
glClearColor(0.0, 0.0, 0.0, 0.0);
[[CCDirector sharedDirector] openGLView].opaque = NO;
From then on you can use the dummyView to manage the view hierarchy. For example, when you add your game over view to the dummyView, you want to call sendToBack on it so that it is drawn behind the cocos2d view.
The entire process with all things to consider is described in detail in my Learn Cocos2D Game Development book (2nd Edition).