Yup, my Cocos2d tutorials are finally updated for v2. So is my upcoming iBook on Cocos2d (still free). It took me a couple days last week to update all the source files for the video course. Not because updating the library and some classes was tough, it’s just a lot of files to go through. Darn me for saving so many versions!!
To my surprise, I only found two very minor bits of code that needed changing from Cocos2d v1 to v2.
When creating a CCMenuItem, itemFromNormalImage is now itemWithNormalImage. For example…
CCMenuItem *button1 = [CCMenuItemImage itemWithNormalImage:@“image1.png” selectedImage:@“image2.png” target:self selector:@selector( someMethod )];
Yeah, not a big change huh.
And then this next change is actually discussed in the videos because I knew it was coming ahead of time. In Cocos2d v1, setting up the Box2D world and sleep property was done like this…
bool doSleep = false;
world = new b2World(gravity, doSleep);
In Cocos2d v2, we do this…
world = new b2World(gravity);
Yay for me. Nothing major that required a video do-over. (Although I did add a video to Session 1 about the IntroLayer class, more on that below).
The source files have all been moved to one zip file with folders for each Session (Session1, Session2, etc), then each Session Part (part1, part2, etc) and then a subfolder for Cocos2d_v1 and Cocos2d_v2. Previously I had the source files included in with each video.
Finally, every Cocos2d v2 project obviously uses the version 2 code library, but also includes now the IntroLayer class which the Cocos2d v2 template includes. This is a nice little class that adds a 1 second startup image to your app which then fades into the usual HelloWorldLayer class. Of course, you can increase the delay or shorten it if you want some extra time before the app really gets under way to show your company’s logo or just give more time to the app’s title card / default image.
If you are using Cocos2d v2, I would recommend keeping this class in there. You could go to your AppDelegate.m file and change IntroLayer to HelloWorldLayer, but I noticed if you do this the init statement of the HelloWorldLayer.m file doesn’t seem to know the right size dimensions if your orientation is set to landscape. Running the lines below will tell you the width is 768 and the height is 1024…
CGSize size = [[CCDirector sharedDirector] winSize];
CCLOG(@”width: %f, height %f”, size.width, size.height);
Whereas, if you run with the IntroLayer in use, the same lines will give you the right sizes. Just FYI !