如何让CAScrollLayer滚动?

时间:2012-04-20 04:19:20

标签: objective-c cocos2d-iphone scroll ccscrolllayer

我正在尝试让CCScrollLayer在我的cocos2d应用程序中运行,但它不会滚动。

这是我做的:

  1. 使用cocos2d模板,创建了一个iOS cocos2d项目。添加了CCScrollLayer文件,并导入到我的HelloWorldLayer类中。添加了一个方法“layerWithLevelName”来创建图层。

  2. 在init中,我创建了几个用于测试的层。下面是我的HelloWorldLayer实现的代码。这是非常基本的,因为我只想尝试滚动一些图层。

  3. // 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
    

    如果您运行代码,您会发现可以看到关卡/滚动图层 - &gt;它似乎已正确加载。但你不能滚动。我忘了做什么?我做错了什么?

    编辑:我正在使用cocos2d-iphone 1.1beta2

1 个答案:

答案 0 :(得分:1)

这似乎是1.1beta2的问题。

临时解决方法是设置scroller.stealTouches = NO;

我不知道这有什么副作用。

编辑:

修复:使用更新的CCTouchDispatcher文件:

https://github.com/cocos2d/cocos2d-iphone/blob/develop/cocos2d/Platforms/iOS/CCTouchDispatcher.m 和相应的.h文件 https://github.com/cocos2d/cocos2d-iphone/blob/develop/cocos2d/Platforms/iOS/CCTouchDispatcher.h