CCDirecter replace scene with UIView / nib - iphone

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.

Related

SWIFT - Can I have a SKView as a subview of a UIView?

In my storyboard, I have a base UIView, then a UIView containing a square board drawing in a CGRect and buttons and status fields above and below. I tried drawing the playing pieces but animating them became a nightmare so I have rendered them as sprite nodes in an array and they animate themselves. So far so good. Then layer on top of that is a SKView view and I want to present the SpriteKit scene in there with skView.allowsTransparency = true so I just have the sprites over a transparent background (revealing the board below) and using TapGestureRecognizers to effect the sprite animation event handling.
So in IB hierarchy is: VC / UIView / boardView: UIView / skView
Problem is, the Spritekit scene always targets the top level UIView not skView so game pieces are behind the board. Am I missing something simple here?
I'm gonna try placing a child VC where skView is but any assist, if I've missed a trick, would be very much appreciated.
I wish I could see some code, but what you do is create an IBOutlet for your SKView, then have that outlet present the scene, I am currently doing this in an app I am working on, so I know it is doable

How to rotate CCUIViewWrapper view with cocos2D

Using CCUIViewWrapper class in cocos2D for creating custom UITableView in my game.Problem is when game is rotating scene is rotating well but by table view is not rotating.
So , i want to know how to rotate CCUIViewWrapper view with cocos2D, perhaps in method like shouldAutorotateToInterfaceOrientation for ios less than 6 and for ios6 shouldAutorotate.
The thing is that the UIKit object isn't part of the cocos2D view, so when you rotate the cocos2D view, it is independent of the UIKit object.
I suspect that you could apply a UIView animation to the UITableView like:
[UIView animateWithDuration:0.25
animations:^{
theView.transform = CGAffineTransformMakeRotation(radians);
}];
Within the CCUIViewWrapper implementation you should be able to override the setRotation: message to apply the same rotation to the UIView it contains, and in theory they should stay in sync.
Just remember that cocos2D seems to use degrees and you'll need to transform this to radians for UIKit (or that's my recollection anyway).

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

Preloading Cocos2D textures when a standard UIKit UIView is visible

My app uses Cocos2D for animation and 2D rendering, but the "game lobby" (that's not what it is, but for all intents and purposes) is all standard UIView/UIKit code.
I have a UIViewController subclass that has its main view set in the XIB file as an EAGLView. When I put all the Cocos loading code into viewDidLoad, everything works as expected, except that there is a 1-2 second freeze while Cocos loads all my textures.
I have about 20 textures that I preload into the CCSpriteFrameCache before running my scene - so my animations are smooth immediately.
I tried to move this texture loading code to my view controller's init method -- but when I try this, Cocos crashes, seemingly because there is no OpenGL context to work with.
What I want to do is, while displaying a UIViewController's main view (and not blocking the main thread), I'd like to load textures in the background, and then when finished, transition between that UIView and the OpenGL EAGLView.
I am aware that there is an addImageAsync: call in Cocos now, so it's really just a matter of somehow "passing around an offscreen EAGLView/EAGLContext" that I can eventually stick into Cocos when I am ready to display that view.
Does anyone have any pointers on an elegant solution to this?
The solution to this problem was to create a separate EAGLContext, using which I would load the textures. That EAGLContext needed to share a EAGLSharegroup with the actual GL view that I used to render the content. So the steps worked like this:
Create/load/show my UI View
Create my to-be-used EAGLView
Snag the EAGLSharegroup off that EAGLView
Create a EAGLContext using that sharegroup
Load my textures using that EAGLContext
Switch my UIView to the EAGLView
Textures are loaded and available

What's the difference for using CALayer and UIView when do iPhone game development?

For iPhone game development, seems there are very like functions using CALayer and UIView, we can implement our spirit using CALayer or UIView , and animations are usable for them too. But for coding, there are many difference between CALayer and UIView like object methods and properties etc. So which is the better choice?
Every UIView is layer-backed, so you can get a layer reference for any view. If you need it to have UIResponder characteristics (e.g., respond to events), you should probably use a UIView.