解析HelloWorldScene

HelloWorldScene 
The HelloWorldScene class is where pure cocos2d code does its magic to display the 
Hello World Label. Before I get into that, you should understand that cocos2d uses a 
hierarchy of CCNode objects to determine what is displayed where.  
The base class of all nodes is the CCNode class, which contains a position but has no 
visual representation. It’s the parent class for all other node classes, including the two 
most fundamental ones: CCScene and CCLayer. 
CCScene is an abstract concept and does nothing more than to allow proper placement 
of objects in the scene according to their pixel coordinates. A CCScene node is thus 
always used as the parent object for every cocos2d scene hierarchy. 
The CCLayer class does very little by itself other than allowing touch and accelerometer 
input. You’ll normally use it as the first class added to the CCScene, simply because most 
games use at least simple touch input. 
If you open the HelloWorldScene.h header file, you’ll see that the HelloWorld class is 
derived from CCLayer. 
Since CCScene is merely an abstract concept, the default way of setting up a scene has 
always been to use a static initializer +(id) scene in your class. This method creates a 
regular CCScene object and then adds an instance of your class to the scene. In almost 
all cases that’s the only place where a CCScene is created and used. The following is a 
generic example of the +(id) scene method: 
+(id) scene 
{ 
        CCScene *scene = [CCScene node]; 
        id node = [HelloWorld node]; 
        [scene addChild:node]; 
 
        return scene; 
} 
First, a CCScene object is created using the static initializer +(id) node of the CCScene 
class. Next, our HelloWorld class is created using the same +(id) node method and 
then added to the scene. The scene is then returned to the caller.
Moving on to the –(id) init method in Listing 2–2, you’ll notice something that might 
seem odd: self is assigned the return value from the init message sent to the super 
object in the call to self = [super init]. If you come from a C++ background, you’ll 
shudder in pain looking at this. Don’t get too upset, it’s alright. It simply means that in 
Objective-C we have to manually call the super class’s init method. There is no 
automatic call to the parent class. And we do have to assign self the return value of the 
[super init] message because it might return nil:  
Listing 2–2. The init Method Creates and Adds the Hello World Label 
-(id) init 
{ 
        if ((self = [super init])) { 
                // create and initialize a label 
                CCLabel* label = [CCLabel labelWithString:@"Hello World"  
                                                   fontName:@"Marker Felt" fontSize:64]; 
 
                // get the window (screen) size from CCDirector 
                CGSize size = [[CCDirector sharedDirector] winSize]; 
         
                // position the label at the center of the screen 
                label.position =  CGPointMake(size.width / 2, size.height / 2); 
 
                // add the label as a child to this Layer 
                [self addChild: label]; 
        } 
        return self; 
} 
If you’re deeply concerned by the way Objective-C programmers write the call to [super 
init], here’s an alternative that might ease your mind. It’s fundamentally the same, just 
not what tradition dictates: 
-(id) init 
{ 
        self = [super init]; 
        if (self != nil) { 
                // do init stuff here … 
        } 
        return self; 
} 
Now let me explain how the label is added to the scene. If you look again at the init 
method in Listing 2–2, you’ll see that a CCLabel object is created using one of init’s 
static initializer methods. It’ll return a new instance of the CCLabel class as an 
autoreleased object. To not have its memory freed after control leaves the init method, 
you have to add the label to self as child using the [self addChild:label] message. In 
between, the label is assigned a position at the center of the screen. Note that whether 
you assign the position before or after the call to addChild doesn’t matter.  

《Learn iPhone and iPad Cocos2D Game Development》


翻译转载:(原)cocos2d笔记——解析HelloWorldScene

HelloWorldScene类是纯coocs2d代码显示Hello World标签的地方。在开始深入这个之前,你要先明白cocos2d使用了一个CCNode对象层级来确定在什么地方显示什么东西。所有node的基类是CCNode类,CCNode类是所有其他node类的父类,包括两个最基本要素:CCScene和CCLayer。

CCScene是一个抽象概念,只做一件事——根据对象的像素坐标给予对象在场景中的正确位置。CCScene node一直被用作cocos2d场景层级的父对象。

CCLayer类除了提供触摸和加速器输入外基本没干什么事,你通常会将作为第一个类添加到CCScene中,仅因为绝大多数的游戏至少要有简单的触摸输入.如果你打开HelloWorldScene.h头文件,你会发现HelloWorld类源自CCLayer。

因为CCScene只不过是个抽象概念,所以设置场景的默认方式就是在你的类中使用静态初始化器+(id) scene 。这个方法创建了一个常规的CCScene对象并向场景中添加了一个你的类的实例。绝大多数情况下,那是CCScene创建和使用的唯一地方。接下来是一个+(id) scene方法的普通例子。

 
    
+ (id) scene { CCScene * scene = [CCScene node]; id node = [HelloWorld node]; [scene addChild:node]; return scene; }

来到下面代码中的-(id) init方法,你会注意到有些地方很奇怪:self被指定为返回值,这个返回值通过self = [super init]中发送给super对象的init消息得来,如果你有C++背景,看到这个一定会痛苦得打哆嗦。不要太心烦,没关系的。它只意味着在Object-C中我们必须手工调用父类得init方法。没有对父类的自动调用。我们必须指定self为[super init]的返回值,因为它可能会返回nil。

-(id) init
{
 if ((self = [super init])) {
// create and initialize a label
CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
// get the window (screen) size from CCDirector 
CGSize size = [[CCDirector sharedDirector] winSize];

// position the label at the center of the screen 
label.position = CGPointMake(size.width / 2, size.height / 2);

// add the label as a child to this Layer 
[self addChild: label];
} return self;
}
现在让我解释一下标签是怎么加入到场景中的。如果你再看一下上面代码的init方法,你会看到init的一个静态初始化方法创建了一个CCLabel对象,它返回了一个新的CCLabel类的对象,这是个自动释放对象。为了使得init方法结束后不再占用内存,你必须将标签作为孩子添加到self中,这个通过调用[self addChild:label]消息的方式完成。在上面代码所示的方法体里面,标签被指定在屏幕中心的位置。注:你指派位置在调用addChild前还是后没有关系。

 

你可能感兴趣的:(mac)