Search code examples
cocos2d-iphonespritebuilder

What is the difference between `Subclass of CCScene` and `Subclass of CCNode` in cocos2d-iphone?


What is the difference between Subclass of CCScene and Subclass of CCNode when adding new cocos2d CCNode class? I used CCScene when I was following cocos2d-tutorials, but in the cocos2d + SpriteBuilder project default MainScene class is subclass of node, so can somebody explain it to me?


Solution

  • Very little. Open CCScene's header file and you see it subclasses CCNode. If you open CCScene's implementation file you'll essentially see this (assuming we are talking about cocos2d v3):

    @implementation CCScene
    
    // -----------------------------------------------------------------
    
    // Private method used by the CCNode.scene property.
    -(BOOL)isScene {return YES;}
    
    -(id)init
    {
        if((self = [ super init ]))
        {
            CGSize s = [CCDirector sharedDirector].designSize;
            _anchorPoint = ccp(0.0f, 0.0f);
            [self setContentSize:s];
    
            self.colorRGBA = [CCColor blackColor];
        }
    
        return( self );
    }
    
    // -----------------------------------------------------------------
    
    - (void)onEnter
    {
        [super onEnter];
    
        // mark starting scene as dirty, to make sure responder manager is updated
        [[[CCDirector sharedDirector] responderManager] markAsDirty];
    }
    
    // -----------------------------------------------------------------
    
    - (void)onEnterTransitionDidFinish
    {
        [super onEnterTransitionDidFinish];
    
        // mark starting scene as dirty, to make sure responder manager is updated
        [[[CCDirector sharedDirector] responderManager] markAsDirty];
    }
    
    // -----------------------------------------------------------------
    
    @end
    

    What you see above is the scene's implementation. It is essentially a node with the content size set to the design size (view size if it is not set), an anchor point of (0,0), a flag that marks it as a scene (which is privately used by CCNode), and a black color for its RGBA. A CCNode does not assume this stuff (for example content size of a node is 0,0 whereas the scene has a a content size of the view/design).

    Nodes are the base class for many cocos2d classes. A scene, as you see, is just a node with a meaningful name, a set content size the size of the view (unless you specify a design size), and a black color. On the other hand a sprite is also a node but with other properties related to sprites.

    I haven't bothered to use Sprite Builder, but I'd assume by what you've wrote that everything is added to a CCNode, which is ultimately added to a CCScene. Since there is no CCLayer, I'd assume it is simply just using a CCNode to act as the default layer. Someone else can confirm since I've never had a reason to use Sprite Builder but that sounds like it would be the case.