2011-06-14 108 views
3

有没有人知道获取CCMenuItem对齐到网格的最佳实践方法?这是一个问题的cocos2d将CCMenu对齐到网格

例如:

int levelCount = 10; 

CCMenu *menuArray = [CCMenu menuWithItems:nil]; 

for (int x = 1; x<=levelCount; x++) { 
    CCLOG(@"Creating level icon for Level %i", x);  
    [menuArray addChild:[CCMenuItemImage itemFromNormalImage:@"Button2n.png" 
               selectedImage:@"Button2s.png" 
                 target:self 
                selector:@selector(onPlay:)]]; 

} 

[menuArray alignToGridWouldbeGreat????!!!!]; 
[self addChild:menuArray]; 

我可以对齐垂直,水平,在列或行然而不能完成一列或一行配置。

在此先感谢!

+0

嗯在这里我们去,我会试试这个http://www.cocos2d-iphone.org/forum/topic/3194 – Timbo 2011-06-14 22:14:03

回答

7

你只需要调用一个重载的alignItemsInColumns或alignItemsInRows方法。例如,如果你有15个菜单项,并希望3行5列,这样做:

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

唯一的缺点是,似乎没有成为一个办法设置填充对准网格时。

+0

这里的填充问题的一个解决方案是向该方法添加一个“紧密”变量,如下所示: - (void)alignItemsInColumns:(NSNumber *)列vaList:(va_list)args和Tightness :(浮动)紧密度。您在设置项目位置的行中使用“紧密度”:w = winSize.width /(1 + rowColumns)/ tightness;它适用于我:) – Eddy 2013-07-22 08:15:42

+0

填充是在这个cocos2d-x文章中管理的,它对我很有用:https://github.com/cocos2d/cocos2d-x-for-xna/issues/20 – Zappescu 2015-02-05 17:47:00

0

可能是你可以试试这个....

[menuArray alignItemsVerticallyWithPadding:20.0]; 

[first_menu alignItemsHorizontallyWithPadding:20.0]; 
+0

我不认为这些选项允许多个行或列,这是什么即时通讯之后。例如,如果我有16个项目,一个4x4网格。该问题已被提供的链接中的其他人问,但答案链接已经死了http://www.cocos2d-iphone.org/forum/topic/8310 – Timbo 2011-06-14 21:15:16

0

据我所知阿尼什的回答是行动的你最好的课程。这将与对齐网格相同,这是我个人使用的。只需设置你的菜单位置和对齐填充,你应该有你在找什么。

+0

我不认为这些选项允许多个行或列,这是什么后即时。例如,如果我有16个项目,一个4x4网格。这个问题已经被提供的链接中的其他人问过,但是答案链接已经死了http://www.cocos2d-iphone.org/forum/topic/8310 – Timbo 2011-06-14 21:16:14

+0

噢好吧...然后不完全确定。如果您在遵循论坛主题“3194”中讨论的方法时遇到问题,那么您总是可以创建多个菜单,每个菜单中只有一个项目。然后,您可以将菜单放在任何你喜欢的地方。 – 2011-06-15 00:24:28

1

好的,虽然没有我想要的那么灵活,但我已经为我的目的找到了足够体面的解决方案。任何人都可以随意使用此代码,如果他们也发现它有用。

//////// Put images (or whatever) for all levels in an array /////////   

    int levelCount = 15; 
    NSMutableArray* menuArray = [NSMutableArray arrayWithCapacity:levelCount]; 
    for (int x = 1; x<=levelCount; x++) { 

     CCLOG(@"Creating level icon for Level %i", x); 

     CCMenuItemImage* item = [CCMenuItemImage itemFromNormalImage:@"Button2n.png" 
                 selectedImage:@"Button2s.png" 
                   target:self 
                  selector:@selector(onPlay:)]; 
     [menuArray addObject:item];       
    } 

////////与列/////////

CGSize screenSize = [CCDirector sharedDirector].winSize; 

    int columns = 5; 

    int spaceBetweenColumns = columns + 1; 

    int spacing = screenSize.width/spaceBetweenColumns; 

    CCLOG(@"screenWidth (%f)/columnsWithEdges (%i) = spacing = %i, ", screenSize.width, spaceBetweenColumns, spacing); 

    CGPoint currentDrawPoint = CGPointMake(0, screenSize.height - spacing); // start at the top 

    for (CCMenuItem *item in menuArray) { 

     currentDrawPoint.x = currentDrawPoint.x + spacing; 

     if (currentDrawPoint.x > (columns * spacing)) { 
      // start a new line as we have reached the end of the previous one 
      currentDrawPoint.x = spacing; 
      currentDrawPoint.y = currentDrawPoint.y - spacing; 
     } 

     item.position = currentDrawPoint; 
     [self addChild:item]; 

    } 
1

这里的具体数字网格安排是我的解决方案,我希望它能帮助。

首先定义这个结构的地方:

typedef struct 
{ 
int cols; 
}RowInfo; 

然后:

-(void)layoutMenu:(CCMenu *)menu rowInfo:(RowInfo[])inf rows:(int)rows padding:(CGPoint)padding  
{ 
CCMenuItem *dummy = (CCMenuItem *)[menu.children objectAtIndex:0]; 
int itemIndex = 0; 

float w = dummy.contentSize.width; 
float h = dummy.contentSize.height; 

CGSize screenSize = [[CCDirector sharedDirector]winSize]; 
CCArray *items = [menu children]; 

float startX; 

for (int i = rows - 1; i >=0; i--) 
{ 
    int colsNow = info[i].cols; 

    startX = (screenSize.width - (colsNow * w + padding.x * (colsNow - 1)))/2; 
    float y = i * (padding.y + h); 

    for (int j = 0; j < colsNow; j++) 
    { 
     CCMenuItem *item = (CCMenuItem *)[items objectAtIndex:itemIndex]; 
     item.anchorPoint = ccp(0,0); 
     item.position = ccp(startX, y); 
     startX += padding.x + w; 
     itemIndex++; 
    } 
} 
} 

的调用是这样的(自定义键盘):

//create custom keyboard 
NSArray *captions = [NSArray arrayWithObjects: 
@"Q", @"W", @"E", @"R", @"T", @"Y", @"U", @"I", @"O", @"P", 
    @"A", @"S", @"D", @"F", @"G",@"H", @"J", @"K", @"L", 
    @"Z", @"X", @"C", @"V", @"B", @"N", @"M", nil]; 

CCMenu *menu = [CCMenu menuWithItems:nil]; 

[self addChild:menu]; 

for (NSString *caption in captions) 
{ 
    CCLabelTTF *label = [CCLabelTTF labelWithString:caption fontName:@"Courier" fontSize:25]; 
    CCMenuItemLabel *item = [CCMenuItemLabel itemWithLabel:label target:self selector:@selector(callDelegate:)]; 
    [menu addChild:item]; 
} 

RowInfo info[3] = {{7}, {9}, {10}}; //inverse order 

[self layoutMenu:menu withRowInfo:info rows:3 padding:ccp(15, 15)];