March 18, 2011 7

Combining Cocos2D with UIKit and UIToolbar

By admin in Uncategorized

Cocos2D is an awesome 2D iphone game engine, it’s just really really good and the code inside is nicely organized.
However, to a beginner using Cocos2D it would seem that it does not work with UIKit based applications, this impression is largely Cocos2D’s fault.

I recently started a new game, and my plan is to use UIKit for some of the level-creation and imo there’s no need to use Cocos2D to simulate UIKit when UIKit is already an amazing UIKit simulator and readily available.

Last time I posted a similar idea, a lot of people found it useful, I started doing a write-up but to be honest it involves many steps that need explaining, and i’m not great at it.

So this time i’m instead providing it as a xcode project which you can open and compile and just start using!

Download / Repository

https://github.com/onedayitwillmake/Cocos2D-UIToolbar

Preview

Slight detail

  • Cocos2D lives in side a EAGLView, and EAGLView is just a view that you can insert into other views!
  • Especially in recent versions of iOS it plays very very nicely with other UIView’s occupying the screen at the same time
  • I started off with Cocos2D Box2D template, put the default logic in RootViewController inside of a new UIViewController and made sure Cocos2D was not attaching itself to the window.

Tags: ,

7 Responses to “Combining Cocos2D with UIKit and UIToolbar”

  1. horseshoe7 says:

    This is by far one of the most useful bits of example code I’ve got my hands on in a long time. I wouldn’t have known how to do this otherwise and it just recently became a requirement of my project! THANK YOU!

    Just got my Cocos3D scene up and running, and it runs at 60FPS. Awesome!

  2. admin says:

    Awesome, glad it helped. They play surprisingly well together, and once you get your head around it it’s quite easy

  3. JDT says:

    Great working example of this kind of nesting! I’ve just begun to use this for my project, and it seems to be working really well. The only problem I’ve run into so far is that menu buttons in the cocos2d window seem to be unresponsive after a scene change. That is, in my first scene containing a menu, they seem to work just fine, but after a replaceScene to a new screen, the menus there are non-responsive, while touches in general seem to continue working fine. Any ideas?

  4. JDT says:

    Disregard my last comment. I’ve actually narrowed the problem and it’s even more bizarre. In the embedded application, the only touches that go through are ones on the top half of the display screen. Touches below the halway point down the iPhone screen are never picked up by the application! This behavior is even more bizarre, but considerably more amusing. Still I ask, any ideas?

  5. JDT says:

    It’s worth mentioning that this behavior with the bottom half of the screen being nonresponsive happens both in the base AirHop application you have provided and in my application using this embedding method. Very strange behavior.

  6. admin says:

    Hi JDT – Yes I noticed the same thing, and I figured out what I was doing wrong however it was part of another project. I’m going to remember what I did, but if you want to take a stab it it for now, it is related to the viewports and whether Cocos2D thinks it’s in landscape or not.

    I’m gonna take a look tomorrow, I remember on the other project that it was one of those obvious once you see it bugs.

  7. JDT says:

    I poked around with it, but I was unable to find the source of the bug. Were you able to isolate it again?

Leave a Reply

Powered by WP Hashcash

Spam protection by WP Captcha-Free