Understanding Layer

What is Layer?
  • A CCLayer has a size of the whole drawable area, and knows how to draw itself. 
  •  It can be semi transparent (having holes and/or partial transparency in some/all places), allowing to see other layers behind it. 
  • Layers are the ones defining appearance and behavior, so most of your programming time will be spent coding CCLayer subclasses that do what you need.
  • Each Scene contains several Layers
  • The CCLayer is where you define event handlers and where user interacts with Cocos2d objects. 
  • Events are propagated to layers (from front to back) until some layer catches the event and accepts it. 
  • Although some serious apps will require you to define custom CCLayer classes, cocos2d provides a library of useful predefined layers ( a simple menu layer: CCMenu, a color layer: CCColorLayer, a multiplexor between other layers: CCMultiplexLayer, and more ).
  • Layers can contain CCSprite objects, CCLabel objects and even other CCLayer objects as children.
  • Since layers are subclass of CCNode, they can be transformed manually or by using actions.

  • Each layer (CCLayer) can in turn have sprites (CCSprite), labels (CCLabel), and other objects you want to display onscreen. 
Working Example


#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface BackgroundLayer : CCLayer {



#import "BackgroundLayer.h"
@implementation BackgroundLayer
-(id)init {
    self = [super init];
    if (self != nil) {

        CCSprite *backgroundImage;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            // Indicates game is running on iPad
            backgroundImage = [CCSprite
            backgroundImage = [CCSprite
        CGSize screenSize = [[CCDirector sharedDirector] winSize]; 
        [backgroundImage setPosition:

} else {
         CGPointMake(screenSize.width/2, screenSize.height/2)];
        [self addChild:backgroundImage z:0 tag:0];
    return self;

No comments:

Post a Comment