使用CCLayer的子类用于CCScrollLayer

时间:2012-04-20 11:08:10

标签: ios objective-c ios5 cocos2d-iphone ccscrolllayer

我创建了一个subclass CCLayer的自定义类,并尝试将其用于CCScrollLayer

我这样做的方式是:

//Store the my layers to an NSMutableArray
for (AACustomClassLayer *cardLayer in levels) {
    [layers addObject:cardLayer];
}

CCScrollLayer的引擎盖下,它崩溃了:

- (void) updatePages
{
    // Loop through the array and add the screens if needed.
    int i = 0;
    for (CCLayer *l in layers_)
    {
        l.anchorPoint = ccp(0,0);
        l.contentSize = [CCDirector sharedDirector].winSize;
        l.position = ccp(  (i * (self.contentSize.width - self.pagesWidthOffset)), 0  );
        if (!l.parent)
            [self addChild:l];
        i++;
    }
}

AACustomClassLayer类(CCLayer的子类)的实现如下:

-(id)initWithChapter:(AALevel *)level {
    self = [super init];
    if (self) {
        self.isTouchEnabled = YES;
//Here I'm adding the CCSprite to my layer

  }
    return self;
}

更新:

崩溃日志

2012-04-20 14:12:12.344 [15780:10a03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setAnchorPoint:]: unrecognized selector sent to instance 0x884ab40'
*** First throw call stack:
(0x1a75022 0x200fcd6 0x1a76cbd 0x19dbed0 0x19dbcb2 0xd013f 0xcfe2b 0x102370 0x44c15 0xbe45f 0x8a94be 0x8aa274 0x8b9183 0x8b9c38 0x8ad634 0x282def5 0x1a49195 0x19adff2 0x19ac8da 0x19abd84 0x19abc9b 0x8a9c65 0x8ab626 0xbda06 0x22e5)
terminate called throwing an exception

2 个答案:

答案 0 :(得分:0)

我找到了它!

for (AACustomClassLayer *cardLayer in levels) {
    cardLayer = [[AACustomClassLayer node] autorelease];
    [layers addObject:cardLayer];
}

答案 1 :(得分:0)

您应该在for循环中添加一个条件检查,以确定您从枚举中获取的对象是否实际上是CCLayer。您的崩溃日志指出anchorPoint setter在某个对象上不可用,可能是在layers_数组中的对象上,因为这是您发布的处理anchorPoints的代码。

枚举很方便,但是当有可能其中一个对象没有时,你将所有对象都包含在CCLayer中。我不知道你在layers_添加对象的位置,但你是否可能添加一个实际上不是CCLayer的对象?