微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将CCMenu与网格对齐

如何解决将CCMenu与网格对齐

| 有谁知道使CcmenuItems数组对齐网格的最佳实践方法吗?这是一个cocos2d问题 例如 :
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];
我可以在列或行中垂直,水平对齐,但是不能包装列或行配置。 提前致谢!     

解决方法

        您只需要调用重载的alignItemsInColumns或alignItemsInRows方法之一。例如,如果您有15个菜单项,并且想要3行5列,请执行以下操作:
CCMenu* menu = [CCMenu menuWithItems:...];
NSNumber* itemsPerRow = [NSNumber numberWithInt:5];
[menu alignItemsInColumns:itemsPerRow,itemsPerRow,nil];
唯一的缺点是,在对齐网格时似乎没有办法设置填充。     ,        好的,虽然没有我想要的灵活,但我已经有足够不错的解决方案来满足我的目的。如果其他人也觉得有用,可以随意使用此代码。
//////// 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];

    }
    ,        这是我的解决方案,希望对您有所帮助。 首先在某处定义此结构:
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)];
    ,        也许你可以试试这个。
[menuArray alignItemsVerticallyWithPadding:20.0];
要么
[first_menu alignItemsHorizontallyWithPadding:20.0];
    ,        据我所知,Anish的答案是您最好的行动方案。这与对齐网格相同,这是我个人使用的。只需设置您的菜单位置和对齐填充,您便会找到所需的内容。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。