objective-ccocos2d-iphonescrollccscrolllayer

How to get CCScrollLayer to scroll?


I am trying to get CCScrollLayer to work in my cocos2d app, but it won't scroll.

Here is what I did:

  1. Using the cocos2d template, created a iOS cocos2d project. Added the CCScrollLayer files, and imported into my HelloWorldLayer class. Added a method "layerWithLevelName" to create the layers.

  2. In init, I created a few layers for testing. Below is the code for my HelloWorldLayer implementation. It is very basic, as I am just trying to get some layers to scroll.

#

// HelloWorldLayer implementation
#import "HelloWorldLayer.h"
#import "CCScrollLayer.h"
@implementation HelloWorldLayer


+(CCScene *) scene
{
    CCScene *scene = [CCScene node];    
    HelloWorldLayer *layer = [HelloWorldLayer node];    
    [scene addChild: layer];

    // return the scene
    return scene;
}

-(CCLayer*) layerWithLevelName:(NSString*)name number:(int)number screenSize:(CGSize)screenSize
{
    CCLayer *layer = [[[CCLayer alloc] init]autorelease];

    int largeFont = [CCDirector sharedDirector].winSize.height / 9;
    CCLabelTTF *layerLabel = [CCLabelTTF labelWithString:name fontName:@"Marker Felt" fontSize:largeFont];
    layerLabel.position =  ccp( screenSize.width / 2 , screenSize.height / 2 + 10 );
    layerLabel.rotation = -6.0f;
    layerLabel.color = ccc3(95,58,0);
    [layer addChild:layerLabel];

    return layer;
}

// on "init" you need to initialize your instance
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) 
    {

        // ask director the the window size        
        NSMutableArray* _layers = [[NSMutableArray alloc]init];
        CGSize screenSize = [CCDirector sharedDirector].winSize;  

        for (int i=0; i<3; i++)
        {
            int number = i;
            NSString* name = [[NSString alloc]initWithFormat:@"level%d",i];
            CCLayer* layer = [self layerWithLevelName:name number:number screenSize:screenSize];
            [_layers addObject:layer];

            [name release];
        }

        // Set up the swipe-able layers
        CCScrollLayer *scroller = [[CCScrollLayer alloc] initWithLayers:_layers 
                                                            widthOffset:230];
        [self addChild:scroller];
//        [scroller selectPage:0];

        [scroller release];
        [_layers release];

    }
    return self;
}

- (void) dealloc
{
    [super dealloc];
}
@end

If you run the code, you will find that you can see the levels / scroll layer --> It seems to have loaded properly. But you can't scroll. What have I forgotten to do? What am I doing wrong?

EDIT: I am using cocos2d-iphone 1.1beta2


Solution

  • It seems that this is an issue with 1.1beta2.

    A temporary workaround is to set scroller.stealTouches = NO;

    I don't know what other side effects this has though.

    EDIT:

    Fix: Use updated CCTouchDispatcher files:

    https://github.com/cocos2d/cocos2d-iphone/blob/develop/cocos2d/Platforms/iOS/CCTouchDispatcher.m and corresponding .h file https://github.com/cocos2d/cocos2d-iphone/blob/develop/cocos2d/Platforms/iOS/CCTouchDispatcher.h