首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将CCMenu与网格对齐

将CCMenu与网格对齐
EN

Stack Overflow用户
提问于 2011-06-14 16:30:53
回答 5查看 6.2K关注 0票数 3

有人知道让CCMenuItems数组与网格对齐的最佳实践方法吗?这是一个cocos2d问题

例如:

代码语言:javascript
复制
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];

我可以在列或行中垂直、水平对齐,但不能包装列或行配置。

提前感谢!

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-11-26 07:39:22

您只需调用其中一个重载的alignItemsInColumns或alignItemsInRows方法。例如,如果您有15个菜单项,并且想要3行5列,请执行以下操作:

代码语言:javascript
复制
CCMenu* menu = [CCMenu menuWithItems:...];
NSNumber* itemsPerRow = [NSNumber numberWithInt:5];
[menu alignItemsInColumns:itemsPerRow, itemsPerRow, itemsPerRow, nil];

唯一的缺点是,在对齐到网格时,似乎没有一种方法来设置填充。

票数 7
EN

Stack Overflow用户

发布于 2011-06-16 06:17:03

好吧,虽然不像我想的那样灵活,但我已经有了一个足够好的解决方案来满足我的目的。如果其他任何人也觉得这段代码有用,他们可以随意使用它。

代码语言:javascript
复制
//////// 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];                        
    }

/以特定列数排列网格/

代码语言:javascript
复制
    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
EN

Stack Overflow用户

发布于 2011-09-19 09:25:27

这是我的解决方案,我希望它能有所帮助。

首先在某个地方定义这个结构:

代码语言:javascript
复制
typedef struct
{
 int cols;
}RowInfo;

然后:

代码语言:javascript
复制
-(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++;
    }
}
}

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

代码语言:javascript
复制
//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)];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6340988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档