2011-06-14 110 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)];