alignItemsInColumns替代cocos2d 3.0

时间:2014-09-18 16:38:07

标签: objective-c cocos2d-iphone

我正在创建Level Select,这里是Cocos2d 2.0调用,用于将菜单按钮对齐到网格

CCMenu* menu = [CCMenu menuWithItems:...];
NSNumber* itemsPerRow = [NSNumber numberWithInt:5];
[menu alignItemsInColumns:itemsPerRow, itemsPerRow, itemsPerRow, nil];

在Cocos2d 3.0中有什么替代方法。 CCLayoutBox只有方向..什么是将CCButtons与网格对齐的最佳方法?

2 个答案:

答案 0 :(得分:0)

通过使用一个而不是两个或更多的CCLayoutBox来完成这一点。

根据您的需求和可扩展性,您可以使用垂直(基于列)或水平(基于行)“外部”CCLayoutBox。

在CCLayoutBox中,您将其他CCLayoutBox实例添加为子项,子布局框具有相反的方向。例如,如果基本布局应该是基于列的(外部CCLayoutBox设置为垂直),则应将一个或多个CCLayoutBox与水平对齐添加到另一个CCLayoutBox。每行一个,包含每列的节点。

请注意,外部布局节点是垂直的还是水平的,在单个“单元格”的对齐和间距方面也有所不同。最好做一个简单的测试,看看哪种方式更适合你的目的。

答案 1 :(得分:0)

也许这不是最好的对齐方式,但它对我有用

- (CCNode *)contentNode:(NSMutableArray *)levels
{
    CCNode *node = [[CCNode alloc] init];
    CGSize bounds = [CCDirector sharedDirector].viewSize;

    for (int a = 0; a < kChapterCount; a++)
    {
        int chapterLevelCount = kLevelCount / kChapterCount;
        int chapterRowCount = chapterLevelCount / kRowCount;
        int chapterColumnCount = chapterLevelCount / chapterRowCount;

        CCLayoutBox *layout1 = [[CCLayoutBox alloc] init];
        for (int b = chapterColumnCount-1; b >= 0; b--)
        {
            CCLayoutBox *layout2 = [[CCLayoutBox alloc] init];
            for (int c = 0; c < chapterRowCount; c++)
            {
                int n = chapterLevelCount * a + (chapterRowCount * b + c);
                NSDictionary *level = levels[n];
                int levelNumber = [level[kLevelNumber] intValue] + 1;
                int levelStars  = [level[kLevelStar] intValue];
                BOOL levelLock  = [level[kLevelLocked] boolValue];

                CCButtonAction *button = levelLock ? [self lockedLevel] : [self unlockedLevel:levelNumber star:levelStars];
                [layout2 addChild: button];
            }
            layout2.anchorPoint = ccp(0.5, 0.5);
            layout2.spacing = 10.0f;
            layout2.direction = CCLayoutBoxDirectionHorizontal;
            [layout2 layout];
            [layout1 addChild:layout2];
        }
        layout1.anchorPoint = ccp(0.5, 0.5);
        layout1.spacing = 6.0f;
        layout1.direction = CCLayoutBoxDirectionVertical;
        [layout1 layout];
        layout1.position = ccp(a * bounds.width + bounds.width / 2, bounds.height / 2);
        [node addChild: layout1];
    }

    return node;
}