2014-09-18 70 views
0

我创建水平选择和这里的cocos2d 2.0呼吁对齐菜单按钮,网格alignItemsInColumns替代了cocos2d 3.0

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

什么是替代这个在cocos2d 3.0。 CCLayoutBox只有方向..什么是CCButtons对齐网格的最佳方式?

回答

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; 
} 
0

这很简单,只使用两个或更多的CCLayoutBox。

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

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

请注意,无论外部布局节点是垂直还是水平,在涉及单个“单元”的对齐和间距时也会产生差异。最好做一个简单的测试,看看哪种方式更适合你的目的。

+0

我有5 CCLabelTTF一个CCLayoutBox。我已将CCLayoutBox方向设置为CCLayoutBoxDirectionVertical。我想将所有标签对齐,因为它们应该是沿着设备的左边缘。目前他们在中心对齐。我也试图在代码中使用Sprite Builder来做到这一点。 – Asdrubal 2015-05-10 18:57:33