Cocos 2D in UIKit application? - ios

I have pretty much made my app to about 90% using UIKit code since it is my first app and wanted to keep it simple. Now I learned that I needed to use Cocos 2D in one of my views, my play view. So I have coded half of the functionality in the view using UIKit. Is there any way to use Cocos 2D in that view for the actual game part? Are there any easy tutorials or links someone can point me to, to do this?
Thanks!

I think the link Brad noted is a great reference. More immediately something like....
EAGLView *glview = [EAGLView viewWithFrame:CGRectMake(0, 0, 250,350)];
[yourView addSubview:glview];
CCDirector *director = [CCDirector sharedDirector];
[director setOpenGLView:glview];
Should work for you. Create the OpenGL view for the CCDirector to use. Attach that view as a subview of the view where you want it placed. Attach the CCDirector to your OpenGL view.
Additionally the Cocos2D website and wiki have some great resources that may help...
Cococs2D Wiki - http://www.cocos2d-iphone.org/wiki/doku.php/
Cocos2D Forums - http://www.cocos2d-iphone.org/forum/

Related

How do I use antialiasing programmatically in OpenGl ES Xcode?

So the title pretty much says it already, I use the OpenGl ES template in Xcode to create a game. I use triangles for my code.
So what I want is basically the same like here, just for iOS instead of Android.
Can anyone help me please?
While initializing the GLKView, call this to enable 4x MSAA, with view being your GLKView instance:
[view setDrawableMultisample: GLKViewDrawableMultisample4X];

Display 3D-Object with COCOS3D on top of AR-View

I work on an app, which scans an image and shows you a 3D-Object or a video on top of the image target. Normal AR-App. For that AR stuff I usw the Vuforia SDK. The problem is, that the Vuforia SDK don't support animated 3D-Objects and for that I use cocos3d.
So I created a basic cocos3d app and included my vuforia stuff for the AR. This works good and the app displays normal 3D-Objects and videos. This was the background, now the problem.
The first view I have is my camera view, which scans the images. If I scan now a specified target, I want to show an animated 3D-Object. For that, I display the cocos3d view on top of the AR-View. The cocos3d view is transparent and is displayed on top of the AR-View (tested this with a simple button in the cocos3d view).
The problem is, that I'am not able to display a animated 3D-Object. I tested some options but none of them worked because I don't really have an idea how to do that. My current code:
CCDirector *director = CCDirector.sharedDirector;
EAGLViewCC *glView = [EAGLViewCC viewWithFrame:[window bounds] pixelFormat:kEAGLColorFormatRGBA8 depthFormat:GL_DEPTH_COMPONENT16_OES];
[director setOpenGLView:glView];
[window addSubview:director.openGLView];
After that I have a layer and add the test scene to my layer (Standard from the example). But than I don't know how to display it. It tried this:
[director pushScene:scene];
but no luck. In the example, they use this code to show the object (viewController is of type "CC3DeviceCameraOverlayUIViewController").
[viewController runSceneOnNode: mainLayer];
Why don't I use the viewController? Because I couldn't get the view transparent. So how do I get the 3D-Object displayed in my view? What do I have to do? Am I completely wrong?

Ccocs2d UIScrollView Stops Touch Response

I am using a uiscrollview in my cocos2d project, using modification of some cocos2d files and the great CCScrollView class I got from a SO link. It is working fine but when I move to a next scene, the new scene is not responding to touches (whereas it was working fine before).
I am using iOS 5.1 & cocos2d 2.x.
Any suggestions?
Ok, you need to do this:
[aScrollView removeFromSuperview];
I'm not quite sure why since in the dealloc of CCScrollView you have:
[self removeFromSuperview];
I guess its being retained somehow? Anyway, it works.

icarousel with cocos2d issue

Every body i am integrating icarousel with cocos2d using uiviewwrapper and after that i have done with icarousel is working fine in simulator but on ipad it make some problem that is its first and last options are responding normally as it does but in between options uibuttons are not responding when selected and thay are working when that are at some specific angle not in front view .I have searched alot but i could not find any solution for that as there is not a single warning or error when app builds.
All suggestions will be welcomed. Thanks
https://github.com/nicklockwood/iCarousel/issues/85
In the appDelegate function delete the line
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
and replace the same with
[CCDirector setDirectorType:kCCDirectorTypeDefault];
setting the [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] will cause unexpected behaviour in iCarousel.
It's possible that Cocos may be interfering with the UITapGestureRecognizer that iCarousel uses to intercept taps and forward them to the item views.
That would explain why you can tap the side views but not the frontmost view, since it's gestures are treated differently.
Try poking around in the code and adding some breakpoints to the gesture handling logic to see if you can see where it's failing.
Alternatively, send me a working code example and I'll take a look.

Questions for hacking OpenGL ES2 template project from Xcode 4

Create new project from opengl template via xcode4, you will see one color square moving.
I want to put two more small views to show the same thing. What I did is to add two
EAGLView *glView1;
EAGLView *glView2;
Then set up with property/systhesize etc, and same coding with self.view like these
[(EAGLView *)self.view setContext:context];
[(EAGLView *)self.view setFramebuffer];
if (!glView2) {
glView2 = [[EAGLView alloc] init];
}
[self.glView2 setContext:context2];
[self.glView2 setFramebuffer];
if (!glView1) {
glView1 = [[EAGLView alloc] init];
}
[self.glView1 setContext:context];
[self.glView1 setFramebuffer];
Do same thing for other place which self.view did. Also do the right thing to link within interface builder. (xcode4)
But the result show
I found that the last draw order in drawFrame will decide which window will show
[self.glView1 setFramebuffer];
[self.glView2 setFramebuffer]; // these will not show , but if put last, it will show
[(EAGLView *)self.view setFramebuffer]; // this will work
Does that make sense to explain my purpose and issues ?
Thanks very much for point out the root cause and help me go through this.
You need to read about OpenGL ES 2.0 ("OGLES2.0") . You shouldn't be using multiple EAGLView instances. You need only one, and you use OGLES2.0 commands to draw on it.
OpenGL is not easy to learn, and you will have to invest many hours of your time before you can achieve even simple renders with it. You should seek an external library like cocos2D to help you achieve what you want. Read the tutorials and documentation there carefully, and experiment. It will still take you a lot of time to achieve pleasing results, but far less than if you were using OGLES directly yourselfe

Resources