如何在cocos2d-iphone中使用for循环创建按钮

时间:2014-11-26 16:35:20

标签: cocos2d-iphone

好的,所以我一直在尝试为自定义级别(用户创建的级别)设置一个级别选择场景,所以我需要为每个创建的级别设置一个按钮,我一直在尝试这样做使用for循环,但只显示一个按钮。这是我的代码:

-(void)AddLevelButtons{
    NSArray *levels = [[NSArray alloc]init];
    levels = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:CustomPath      error:nil];
    LevelCount = (int)[levels count];
    for (int counter= 0;counter<LevelCount;counter++) {
        CCButton *tempButton;
        tempButton=[CCButton buttonWithTitle:@"" spriteFrame:[CCSpriteFrame frameWithImageNamed:@"othercategory.png"]];
        tempButton.block= ^(id sender) {
            GameScene *GameNode = [[GameScene alloc]initWithPlistName:[NSString stringWithFormat:@"CustomLevel_%d",counter]withObjIndex:counter];
            [[CCDirector sharedDirector] replaceScene:LevelEditorNode.scene withTransition:[CCTransition transitionFadeWithDuration:.5f]];
        };
        tempButton.positionType=CCPositionTypeNormalized;
        tempButton.position = ccp(counter+2/5,.5f);
        [ScrollingNode addChild:tempButton];
   }
}

我认为可能两个按钮都已创建,并且它们只有相同的位置,但我设置了一个断点,并且只创建了一个按钮。

0 个答案:

没有答案