Game Development Reference
In-Depth Information
If you have programmed with Objective-C before, you may have to overcome the un-
easy feeling of “just letting go.” ARC is not magic, but it can be trusted to do the right
thing 100% of the time. You might want to read Apple's Transitioning to ARC Release
Notes here: http://developer.apple.com/library/ios/#release-
notes/ObjectiveC/RN-TransitioningToARC/Introduction/Intro-
duction.html .
To give you an example, these two lines of code are identical in ARC:
CCSprite* sprite = [CCSprite spriteWithFile:@”file.png”];
CCSprite* sprite = [[CCSprite alloc] initWithFile:@”file.png”];
Before ARC, you actually had to differentiate between allocating an autorelease object
(first line) or a regularly initialized object (second line). With ARC, you should prefer
the first version if available because it's more readable and is faster under ARC. But
either way is fine, and both create an object whose memory is freed when the
sprite variable goes out of scope. It's as simple as that.
The only thing that doesn't change with ARC is that you have to send the al-
loc message to a class to create an instance of that class. This also allocates and ini-
tializes the object's memory.
Changing the World
What good is a template project like HelloWorld if I don't have you tinker with it at
least a little? I'll have you change the world by touching it! How's that for a start?
First you'll make two changes to the init method to enable touch input and to use a
tag value to retrieve the label at a later point. The changes are highlighted in Listing
2-3 .
Listing 2-3 . Enabling Touch and Gaining Access
to the Label Object
-(id) init
{
if ((self = [super init]))
{
 
 
Search Nedrilad ::




Custom Search