Use CCLabelBMFont inside UIViewController Inside Node - iphone

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).

Related

CCDirecter replace scene with UIView / nib

What I would like to do is have the Cocos2d shared director to replace the current scene with a UIView from UIKit (possibly loaded from a nib). The idea being to cleanly transition between Cocos2d and UIKit.
I've seen a few approaches to this problem. Most of them are about mixing UIKit with Cocos2d in the same scene, I'd like to keep them separate. One approach is to add an instance of UIView as a subview of the directors GLView. Like so:
UIView* cocosView = [[CCDirector sharedDirector] openGLView];
[cocosView addSubview:t];
Overlaying a UIView onto a Cocos layer?
This works but I would have to create a blank scene and transition to that first, then add the UIView, then later remove it and transition to whatever other scene. That's modular but a little messy.
A better approach is to wrap a UIView into a node. Something like:
http://www.cocos2d-iphone.org/forum/topic/6889
Is there an official cocos2d extension for this? I'd also like to load the UIView from storyboard/nib if possible. Much obliged.
replace the current scene with a UIView from UIKit
if you are using cocos2d 1.x: http://www.raywenderlich.com/4817/how-to-integrate-cocos2d-and-uikit
if you are using cocos2d 2.x: CCDirector is a subclass of UIViewController, so you can use usual methods. There's a little bug with delta time calculation after stopping/starting animation, but it's easy to fix.

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.

iPhone: Replacing UIView with itself (resetting view)

I have a UIView that represents a game screen. I have a reset button allowing users to replay that level. For the reset though I want to reset ALL UI controls to their default state as upon completion various animations take place that fade in/out text and move around images etc.
What I would like to do is replace the existing UIView with another copy of itself (with animation). How can I do this whilst not breaking the UINavigation stack? When a user presses back on the new screen I want them to goto the original parent UIView and not the game screen that was replaced.
I do something similar using a mask view.
Create a mask view, a UIView set as your starting view (or just an image).
Animate a transition from your game view to your mask view.
Reset your game view.
Remove your mask view, showing your game view.
Doing so your UINavigation stack won't be broken, because you actually use just one active view (game view) and a static mask to cover your view for transition.
On the top of your controller stack you presumably have controller that "owns the game view"?
I also assume that you are using a custom view within your controller to do most of the view stuff.
If this is the case, then you should be able to alloc, init another instance of your game view which will be in default state. Set it's alpha to 0.0f and add it to the view hierarchy, then animate its alpha to 1.0f and in the animation completion block remove and release th sold view that you no longer need.
Alternatively you could define the start condition, positions, values etc. and then just animate back to them when the button is pressed.
Hope this helps :)
I assume you have code to initialize all of you subviews and game-related data members right?
move that code to a separate method and have the reset button call the method.
I usually have a -setupSubviews method on each custom view i create and i call it each time i need to reset my view to its original state, for example:
- (void)setupSubviews
{
_titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self addSubview:_titleLabel];
_activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectZero];
_activityIndicator.hidesWhenStopped = YES;
[self addSubview:_activityIndicator];
_loading = NO;
}

Better way to make MPMoviePlayer with Overlay view

When we creating application with MPMoviePlayer (within custom UIViewController) and with overlay UIViewController on top of movie player what is better:
- Load movie in player with self.view.presentModalView... or self.view.addSubView...
Or is there any other better way. I now work with modal views and everything works except I have to implement some custom rotations. I am looking for more clear way to make this.
Ok maybe some more details. In iPhone app I add player like modal view and add overlay view to it and that works. But this is universal app and it doesn't work on iPad the same way it works on iPhone. In ipad splitView my player is on details side (nonfullscreen), when I double tap player it goes fullscreen. And now player is key window. Here is the biggest issue. I tried to add overlay view as subviw and modal view and I made it but that overlay is not aware of device orientation, and I have to transform overlay elements and this is what I am trying to avoid. Is there any way to detect rotation on that view?
Simple and most elegant solution:
Do not use the "real" fullscreen mode but scale the MPMoviePlayerController's view towards a screen-filling size - MPMoviePlayerController.view.frame = CGRectMake(0.0f, 0.0f, 768.0f, 1024.0f);. Keep the MPMoviePlayerController's view on top of a regular UIViewController's view that allows rotations (addSubview). Keep your custom interface view on top of the MPMoviePlayerController's view (addSubview) and presto, you will get a properly rotating video player no matter if you run it in "fullscreen" or not.
I found one solution but I am not very proud because I this that there is some better way, but this workaround works:
- In project I have on singleton class that contains some global data for me.
- When application start in detail view (UISplitView) willAnimateRotationToInterfaceOrientation method is called first. Here I get current orientation and store it in singleton class.
- Later when I add overlay on top of player I read data from singleton and transform overlay elements.
According to above I did something like this:
mp = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]];
mp.movieSourceType = MPMovieSourceTypeStreaming;
mp.view.frame = CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f);
[self.view addSubview:mp.view];
mp.controlStyle = MPMovieControlStyleNone;
[self.view addSubview:self.myOverlay];
[mp play];
Actually it was so easy! Just choose control style to None and add your low opacity view on the player view.

Put another view above self.imagePickerController.cameraOverlayView?

I want to customize new camera overlay view, so need figure out one way to put new view above self.imagePickerController.cameraOverlayView.
I have tried those ways
[self.view bringSubviewToFront:self.imagePickerController.cameraOverlayView];
[self.imagePickerController.cameraOverlayView bringSubviewToFront:self.view];
[self.view insertSubview:self.view aboveSubview:self.imagePickerController.cameraOverlayView];
But none can work.
Seem that the only way is
self.imagePickerController.showsCameraControls = NO;
Therefore build my own components like iPhone.Camera
I think the idea with the cameraOverlayView is that you use this as a base UIView for anything you'd like to overlay on the camera view. This main overlay view can be transparent, and you just need to place whatever views you want to overlay on the camera as subviews of it.
You can order these subviews however you want within the overlay to achieve the desired visual effects.
Additionally, I recommend watching the WWDC 2010 video session 421 - "Incorporating the Camera and Photo Library in your App", where they go into detail on how to manage camera overlay views. I believe that Apple's PhotoPicker sample application also shows this off.