Review: Cocos2d for iPhone 1 Game Development Cookbook

“Cocos2d for iPhone 1 Game Development Cookbook” is written by Nathan Burba (@nathanburba).

Cocos2d for iPhone 1 Game Development Cookbook
According to the description, it has “over 90 recipes for iOS 2D game development using cocos2d”. and just by looking at it’s Table of Contents, you’d see that this book covers a lot of topics (some advanced techniques that other cocos2d books have not covered). It also provides solutions to some common problems that we developers might encounter during Cocos2d iOS development.

Like in Chapter 1, it covers graphics, but unlike other books that would only teach you how to make sprites and spritesheets, this book even teaches you how to use apply mipmapping so that that when you scale up or down a sprite it wouldn’t look pixelated. It also has a recipe for a cool technique by swapping palettes so that even with limited amount of art, you can create various versions of a sprite (a single baseball player texture can have be used to create different baseball players with different colored shirts and pants etc). Chapter 1 also answers some questions that you might encounter while working on your game, such as how to I play a video clip of my company’s logo or my game’s cutscene (playing video files recipe). It also covers how to render 3D objects, drawing OpenGL primitives, particles, etc.

Chapter 2 is all about User Input on iOS devices, including the usual (tap, hold, drag), making virtual buttons (creating an analog stick recipe and directional pad recipe) and using the accelerometer. One really cool (for me) recipe that’s also included is gestures (you’d be able to tell what shape the user drew on the screen)!

Chapter 3 is about Files and Data. In making games, we’d need ways to store data (just us score, or level design, etc) and this chapter covers different ways for us to do this. This chapter has recipes for reading plist, json, xml data files, it also includes recipes about archiving objects, saving data using plist, SQLite, Core Data, etc.

Chapter 4 is about physics. Cocos2d has two physics engines that we can use Box2d and Chipmunk. This book only covers Box2d (which is the engine that Angry Birds used), it includes detailed explanations and recipes on how to set up your Box2d environment and simulating physics properties. it even includes a recipe on how to make a car drive up a bumpy hill road!

Chapter 5 is about Scenes and Menus. It also includes how to create buttons and labels (with fancy shadow effects even)! It also has a recipe for wrapping the UIKIT and using it in our Cocos2d game.

Chapter 6 is all about Audio: how to play background sound effects, play music, using the iPod music library. But the really fun bits about this chapter is it even teaches you how to record audio, create a MIDI synthesizer, and to top it all off, it even covers spech recognition and text to speech! Imagine all the games and apps that you can make with those things.

Chapter 7 covers AI, including waypoints, flocking using boids and A* pathfinding. it also teaches you how to run lua scripts and use lua scripts for dialog trees.

Chapter 8 is called Tips, Tools and Ports, and sure enough it includes information about tools that can be used for our Cocos2d game development. Best part, it also includes detailed step by step instructions of how to put your game on the AppStore!

Overall this is a good book with a lot of useful and varied information that have not been covered in other books (and are often lacking in online tutorials). Also
the best thing about this book is it comes with complete working code and detailed explanations on how the code works.

Except some of the topics covered in this book are a bit advanced, so this book is not recommended for beginners, basic Objective C and Cocos2d knowledge is required.

Also the author, Nathan is also very active in the Cocos2d forums, if you have any questions about the book you can just ask him, and he almost always responds immediately.

So if you’re interested to learn new things about Cocos2d iOS development, do check it out! Also if you are interested in this book, I am holding a giveaway contest (where I am giving away 3-4 copies of the book), join now (it’s only until this Wednesday). For more details read my previous blog post (http://purplelilgirl.tumblr.com/post/15713172087/cocos2d-for-iphone-1-game-development-cookbook-giveaway)

Unity 3D Game Development by Example Beginner‚Äôs Guide: Game #2

A few months ago, I wrote a blog post on #AltDevBlogADay reviewing a Unity3D book written by Ryan Henson Creighton, Unity 3D Game Development by Example Beginner‚Äôs Guide (buy the book, it’s pretty good, so far), but I only gotten around to Chapter 4.

Unity 3D Game Development by Example Beginner's Guide

So for this blog post, I will go through some of the other chapters, particularly Chapter 5 and 6, which is about making a game called “Robot Repair”.

Chapter 5 will talk about GUI (graphical user interface, pronounced as “gooey”, like marshmallows). And we’ll be making an entire working 2D flip n’ match memory game in the GUI system.

So let’s get started!

First we open Unity and create a new project called “Robot Repair”, we don’t need to import Standard Assets, according to the boss (Mr. Author, Ryan Henson Creighton).

So we have an empty slate (este project).

A Unity scene is a big 3D world, but we would be ignoring that for now. We will be using the GUI layer, which is like “a sheet of glass” in front of our 3D world.

We will be making 2 scenes for this game. A scene is like a level or a screen in our game. So two scenes, one for the title and one for the game game.

(how organized are we)

So we are making the title screen first.

(tada!)

Of course, so far the TitleGUI script is still empty.

And we type some code (este copy paste from the book):

And we get this:

And if you clicked the button, you get a lil message:

(yeah)

The book came with some code files and assets for each chapter, packed as Unity packages, just double click on the file and it’ll import it to your project.

For the title screen, the author has prepared a cute image that we will add as a GUITexture, and so we get this:

(oh, we also changed the position of the button, so it’s front and well, not really center)

And then hook the button to some code that will bring us to the game:

Application.LoadLevel(“game”);

(yes, this short little code will bring us to the next scene, if we have our scenes added to our build settings)

Lalala… okay, time to make the actual game game.

We go to the game scene (just double click on the scene in the Project panel).

And we’ll make a GameScreen game object and a GameScript script (like what we did with the title screen).

Ooh… what codes will we fill this new script with? *copy paste some codes from the book* Okay, and read up on the explanations too.

And we get this!

(whee)

And then we are done with one chapter. Look at us, pretty good, huh.

Mini Tutorial: How to batch replace textures with different file formats in Unity?

In relation to my previous tutorial: Mini Tutorial: How to batch convert image files to PVR (for iPhone app development)?

So you have a Unity project that was previously not planned for iOS (for PC, Mac or web or whatever), and you might not be as conscious of the texture file formats (iOS is probably the only one with so many constraints).

According to Unity’s User Manual:

Use iOS native PVRT compression formats. They will not only decrease the size of your textures (resulting in faster load times and smaller memory footprint), but also can dramatically increase your rendering performance! Compressed texture requires only a fraction of memory bandwidth compared to full blown 32bit RGBA textures.

So after converting all my textures (or creating a copy of my textures in .pvr format using Automator), I’m supposed to change all the material textures in my Unity Project and that’s a lot. So I wrote this tiny Editor script to do that for me. It will even delete the old texture (.tga or .png or whatever) from the Assets folder.

So here it is:

using UnityEngine;
using System.Collections;
using UnityEditor;                      
 
public class ChangeTexturesToPVR : ScriptableWizard
{
¬†¬†¬†¬† [MenuItem (“Project Tools/Change Textures to PVR”)]
    static void MakeFolder ()
    {   ChangeTextures();
    }
 
    static void ChangeTextures()
    {
        string filePath = AssetDatabase.GetAssetPath(Selection.activeGameObject);
¬†¬† ¬†¬†¬†¬† int index = filePath.LastIndexOf(“/”)+1;
        filePath = filePath.Remove(index);
        
        foreach (Transform child in Selection.activeGameObject.transform)
        {    Material[] mats = child.renderer.sharedMaterials;
           
            foreach(Material mat in mats)
            {    string texFilePath = filePath;
¬†¬† ¬†¬†¬†¬† ¬†¬†¬† ¬†¬†¬† texFilePath = texFilePath.Insert(texFilePath.Length, “Texture/”);
¬†¬† ¬†¬†¬†¬† ¬†¬†¬† ¬†¬†¬† if(mat.mainTexture != null)
                    texFilePath = texFilePath.Insert(texFilePath.Length, mat.mainTexture.name);
               
                string oldTexFilePath = texFilePath;
¬†¬† ¬†¬†¬†¬† ¬†¬†¬† ¬†¬†¬† oldTexFilePath = oldTexFilePath.Insert(oldTexFilePath.Length, “.tga”);
               
¬†¬† ¬†¬†¬†¬† ¬†¬†¬† ¬†¬†¬† texFilePath = texFilePath.Insert(texFilePath.Length, “.pvr”);
               
                mat.mainTexture = (Texture)AssetDatabase.LoadAssetAtPath(texFilePath, typeof(Texture));
                AssetDatabase.DeleteAsset(oldTexFilePath);
            }
        }
       
       
    }
}

If your old textures are in “.png”, just change “.tga” in the code above to well, “.png”.

Select the model (.fbx or .obj file) whose materials and textures that you would want to change.

And then go to the menu and click Project Tools > Change Textures to PVR (yes, I created a Menu item for it).

And then voila!

UPDATE:

I edited the code a bit:

using UnityEngine;
using System.Collections;
using UnityEditor;                     

public class ChangeMaterialTexturesToPVR : ScriptableWizard
{
¬†¬†¬†¬† [MenuItem (“Project Tools/Change Material Textures to PVR”)]
    static void MakeFolder ()
    {   ChangeTextures();
    }

    static void ChangeTextures()
    {
        for(int i=0; i < Selection.gameObjects.Length; i++)
        {    if(Selection.gameObjects[i].transform.childCount == 0)
            {    changeTexture(Selection.gameObjects[i].transform);
            }
            else
            {    foreach (Transform child in Selection.gameObjects[i].transform)
                {    changeTexture(child);   
                }
            }
        }
    }

    static void changeTexture(Transform obj)
    {   
        if(obj.renderer)
        {    Material[] mats = obj.renderer.sharedMaterials;

            foreach(Material mat in mats)
            {   
¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† string texFilePath = “”;

¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† if(mat.mainTexture != null)
                {    texFilePath = AssetDatabase.GetAssetPath(mat.mainTexture);

¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† if(!texFilePath.Contains(“.pvr”))
¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† {¬†¬†¬† texFilePath = texFilePath.Remove(texFilePath.IndexOf(“.”));

                        string oldTexFilePath = texFilePath;
¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† oldTexFilePath = oldTexFilePath.Insert(oldTexFilePath.Length, “.tga”);

¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† texFilePath = texFilePath.Insert(texFilePath.Length, “.pvr”);

                        Texture tex = (Texture)AssetDatabase.LoadAssetAtPath(texFilePath, typeof(Texture));

¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† if(tex != null)
                        {    mat.mainTexture = tex;
                            AssetDatabase.DeleteAsset(oldTexFilePath);

                            Debug.Log(texFilePath);
                        }    else
                        {    tex = (Texture)AssetDatabase.LoadAssetAtPath(oldTexFilePath, typeof(Texture));
                            mat.mainTexture = tex;

                            //Debug.Log(oldTexFilePath);
                        }
                    }
                }
            }

            obj.renderer.sharedMaterials = mats;
        }
    }
}

Now, all you have to make sure is that the .pvr and the .tga (you can change this to .png or .tif or .whatever), is in the same folder and that’s it.

That’s about it.

Cocos2d for iPhone 1 Game Development Cookbook Giveaway

UPDATE: This contest is now closed

Like I mentioned in my previous post, Packt published a new Cocos2d book and they asked me to hold a giveaway contest. I’m holding it both here and in the Cocos2d forums (http://www.cocos2d-iphone.org/forum/topic/28099).

So here it is, rules, rules:

  • I will select a winner using random.org ūüôā
  • Contest ends on 12 Noon Pacific Standard Time January 18th.

There, pretty simple right? ūüôā

Also ManiacDev is also holding a book giveaway contest, he is giving away Unity3D and iOS books. If you guys are interested you can check out his blog: http://maniacdev.com/2012/01/giveaway-get-a-unity-3d-or-ios-book-there-will-be-6-winners-enter-here/

That’s about it ūüôā

Cocos2d for iPhone 1 Game Development Cookbook

Packt recently released a new Cocos2d book: “Cocos2d for iPhone 1 Game Development Cookbook” by Nathan Burba.

Cocos2d for iPhone 1 Game Development Cookbook

Overview of Cocos2d for iPhone 1 Game Development Cookbook

  • Discover advanced Cocos2d, OpenGL ES, and iOS techniques spanning all areas of the game development process
  • Learn how to create top-down isometric games, side-scrolling platformers, and games with realistic lighting
  • Full of fun and engaging recipes with modular libraries that can be plugged into your project
  • Over 90 recipes for iOS 2D game development using cocos2d

Link: http://www.packtpub.com/cocos2d-for-iphone-1-game-development-cookbook/book