Now that you have successfully built and deployed your first Cocos2d app, let's see how it actually works. In Xcode, open up IntroScene.m
.
The IntroScene.m file corresponds with the first scene you see when you load up your app. We will go into more detail on scenes in the next chapter, but for now, all you need to know is what a scene looks like. The first part of the init() method is a slightly different method compared to the standard Objective-C init pattern.
- First, assign
self
, and then check to make sure that it did not return nil
. This is a standard Objective-C pattern to guard against a sub-class or super class not initiating properly: - Next, is your first piece of Cocos2d code:
CCNodeColor
is a Cocos2d object that allows you to create and display a rectangle of a single color.
- You create the color node by passing in the color that you want in Cocos2d; colors are represented by the
CCColor
class. For now, you are making a dark gray background, but experiment by changing the color values, and then building and running the app to see the effect. - Once you have created the color node, you need to add it to the scene so that Cocos2d knows how to render it. Until you add it, it will not be visible on screen. You add the background child to self, which in this case is the intro scene.
- Now, you have a solid background color, but the app is still pretty boring. Let's add some text to say '
Hello World
':
CCLabelTTF
is a label class that allows you to add text to your scene. Labels are created by passing in a string, a font name, and a font size, and then setting a position type. Position types will be covered in the next chapter as well, but a normalized position type allows you to position your node with a percentage from the left and bottom, rather than a fixed position. This is of great value when you are developing an app that will run on multiple screen sizes, such as Android phones and tablets. Set the label color to red and then set the position to (0.5
, 0.5
) using ccp
, which is Cocos2d shorthand for making a new CGPoint
array. Points in Cocos2d have an origin at the bottom-left of the scene. Remember that this is a percentage, so we are placing it 50 percent in and 50 percent up, which is the center of the screen. Once you have finished setting up your label, you add it to the scene so that it will be rendered.
Now you need a way to get to the next scene, where your game will have some interaction. You need to add a button:
CCButton
is a button node that gives you a target and a selector for when the node is tapped on. You can also set a block to run on tap instead, but in this example, we are using the target
/ selector
paradigm. You create the button in a similar way to the label with a string, font name, font size, and position. The difference now is that you also need to set the target and selector. You will need to set the target to self
, and run a method that is in this class, which for this example is onSpinningClicked
. Add this button to the scene to be rendered as well.
Let's have a look at the method that is called when you tap the button:
In this method, you are making a call to the CCDirector
: the director of the game that manages the scene currently on the screen to replace the current scene with the HelloWorld
scene. We will use a transition to do this, which will be covered fully later in this book. For now, we will start with a simple transition that will push the new scene that comes in from the left. You don't have to use a transition, but can add a nice bit of polish to your game.
The HelloWorldScene.m class
Let's take a look at the scene you have transitioned to. If you play around with the app you will see that you have an image that is rotating, and that starts in the center of the screen. When you tap the screen, it moves to where you tapped. Let's see how this works.
- Starting in the
init
method, the first part is always the same, but there is something new now:Enabled user interaction tells the CCDirector
class that you want the scene to receive touches. This is so that when you tap the screen, your image moves.
- Next, you create the background in the same way as in
IntroScene.m
. The following code is another new concept:You are now creating the image that starts in the middle of the screen. These images are known as sprites, and they are created using their image names. Using the names makes Cocos2d look for an image in the app bundle, with the name you provide. You save the sprite reference in an instance variable, so it can be moved around the screen. Then, the position of the sprite is set to start in the center of the screen. Note that this is different to the position type that was used before; now you are setting it to a fixed coordinate rather than a percentage. Then, add the sprite to the scene to be rendered.
- Now, you will add an animation to your sprite in order to make it spin:
This code creates a CCActionRotateBy action with a duration of 1.5 seconds and an angle of 360 degrees clockwise. This means that you want the sprite to rotate once by 360 degrees, and take 1.5 seconds to complete the rotation. You will notice that in the app, the rotation runs continuously; this is achieved on the next line with the relatively self-explanatory CCActionRepeatForever action. You then run the action on your sprite in order to start the rotation. There are many different types of CCAction that will be covered in this book; we have only just touched the surface of what is possible with this example:
- Next, you create a button that will take you back to the main menu. This works exactly the same as in the previous scene.
- Now, let's look at how you handle touch. Scroll down to the code and find the
touchBegan
method.Note
In Xcode, you can use Ctrl+6 and start typing the method you are looking for. This is a quick way to navigate code.
How does touchBegan
work? It gets activated when you first touch the screen, and you get a location of touch translated into the coordinate space of your node. This is an important step because UIKit (the framework used by iOS) uses a different coordinate space to Cocos2d and OpenGL. If the location doesn't get translated, you would end up moving your node to the wrong position on the screen.
Tip
It is important to note the difference between touchBegan
and touchEnded
. Choosing the correct one depends on what you are trying to achieve with your UI. If you want an action to occur as soon as the user touches the screen, then use touchBegan
. If you want an action to occur when the user lifts their finger, then use touchEnded
.
The template then logs the position that you are moving the sprite to, using a CCLog – a
macro helper that allows you to disable logging in release builds.
Next, you will create a CCActionMoveTo
action. This action is similar to CCActionRotateBy
, but now you are moving a node to an identified position rather than rotating a node by an angle. You want your sprite to move to the touch position, using the touch location that was translated. For this example, you want the move to take 1 second, but feel free to change the duration and rebuild to see the effects. Once you have created the action, run it on the sprite. Note that you are using the instance variable that was created in the init
method.