【问题标题】:How to make CCScrollLayer have multiple avatars per page如何让CCScrollLayer每页有多个头像
【发布时间】:2012-02-04 17:57:10
【问题描述】:

我有一个大约 10 个头像的列表,我正在使用 CCScrollLayer 来显示分页。目前,它每页只显示 1 个头像,我更喜欢每页显示 3 个头像,但我不确定如何执行此操作。

我已尝试确保仅在 MODULUS 为 3 时才生成新页面,但这会导致问题,因为部分代码需要可用,例如向菜单中添加内容。

当我尝试使用 MODULUS(绑定到 if 语句)时,它抱怨我的菜单超出范围。

我的代码如下;

// Avatars are generally 70x72
//
GameStateManager *state = [GameStateManager sharedGameStateManager];        
NSLog(@"listOfPlayers.size = %d", [state.listOfPlayers count]);

// Menu of playable characters        
int i=0;

NSMutableArray *pagesArray = [NSMutableArray array];

// --


for (Player *p in state.listOfPlayers) 
{


    // create a blank layer for page
    CCLayer *page = [CCLayer node];
    [page setContentSize:CGSizeMake(200, 100)];

    CCMenu *menu = [CCMenu menuWithItems:nil];
    [menu setContentSize:CGSizeMake(200, 72)];
    [menu alignItemsHorizontallyWithPadding:9.0f];
    [page addChild:menu];


    // --

    NSLog(@"p: %@ (%@) -- locked: %d, playable: %d", p.name, p.fileName, [p.isLocked intValue], [p.isPlayable intValue]);
    //int isLocked    = [p.isLocked intValue];
    int isPlayable  = [p.isPlayable intValue];
    NSString *fileName = [NSString stringWithFormat:@"hold_%@", p.fileName];

    //if ( (isLocked == 0) && (isPlayable == 1) )
    if  (isPlayable == 1) 
    {

        CCSprite *avatarOff = [CCSprite spriteWithSpriteFrameName:fileName];
        CCSprite *avatarOn = [CCSprite spriteWithSpriteFrameName:fileName];

        CCMenuItemSprite *menuItem = [CCMenuItemSprite itemFromNormalSprite:avatarOff selectedSprite:avatarOn target:self selector:nil];
        [menuItem setTag:i];
        [menu addChild:menuItem];

        [pagesArray addObject:page];

        i++;
    }

} // next


// Now create the scroller and pass-in the pages (set widthOffset to 0 for fullscreen pages)
CCScrollLayer *scroller = [[CCScrollLayer alloc] initWithLayers:[NSMutableArray arrayWithArray:pagesArray] widthOffset: 200];


// finally add the scroller to your scene
[self addChild:scroller];

屏幕截图如下。每页显示 1 个头像。

【问题讨论】:

    标签: cocos2d-iphone scroll custom-paging ccscrolllayer


    【解决方案1】:

    在我看来,您正在看到您正在编程的内容。试试

    // Avatars are generally 70x72
    //
    GameStateManager *state = [GameStateManager sharedGameStateManager];        
    NSLog(@"listOfPlayers.size = %d", [state.listOfPlayers count]);
    
    // Menu of playable characters        
    int i=0;
    
    NSMutableArray *pagesArray = [NSMutableArray array];
    
    // --
    
    CCLayer *page=nil;
    CCMenu *menu=nil;
    int avisOnPage=0;
    
    
    for (Player *p in state.listOfPlayers) 
    {
    
      if(0==avisOnPage) {
        // create a blank layer for page
        page = [CCLayer node];
        [page setContentSize:CGSizeMake(200, 100)];
    
        menu = [CCMenu menuWithItems:nil];
        [menu setContentSize:CGSizeMake(200, 72)];
        [menu alignItemsHorizontallyWithPadding:9.0f];
        [page addChild:menu];
        [pagesArray addObject:page];
      } // if new page
    
    // --
    
      NSLog(@"p: %@ (%@) -- locked: %d, playable: %d", p.name, p.fileName, [p.isLocked intValue], [p.isPlayable intValue]);
      //int isLocked    = [p.isLocked intValue];
      int isPlayable  = [p.isPlayable intValue];
      NSString *fileName = [NSString stringWithFormat:@"hold_%@", p.fileName];
    
      //if ( (isLocked == 0) && (isPlayable == 1) )
      if  (isPlayable == 1) 
      {
    
        CCSprite *avatarOff = [CCSprite spriteWithSpriteFrameName:fileName];
        CCSprite *avatarOn = [CCSprite spriteWithSpriteFrameName:fileName];
    
        CCMenuItemSprite *menuItem = [CCMenuItemSprite itemFromNormalSprite:avatarOff selectedSprite:avatarOn target:self selector:nil];
        [menuItem setTag:i];
        [menu addChild:menuItem];
        avisOnPage++;
        i++;
    
        if(3==avisOnPage) avisOnPage=0;
      } // if isPlayable
    } // for player
    
    
    // Now create the scroller and pass-in the pages (set widthOffset to 0 for fullscreen pages)
    CCScrollLayer *scroller = [[CCScrollLayer alloc] initWithLayers:pagesArray widthOffset: 200];
    
    
    // finally add the scroller to your scene
    [self addChild:scroller];
    

    【讨论】:

    • 我收到一个错误,上面写着[menu addChild:menuItem];,因为NewGameLayer.mm:103: error: 'menu' was not declared in this scope
    • 我尝试将它包装在 if 语句中,但没有任何乐趣。并且我将菜单从 if 语句中移出,但只出现了几个头像。
    • true,已将菜单声明移至页面附近。在我提出的逻辑中,每个页面都会有一个“菜单”。如果您需要不同的...
    • 快到了!化身彼此层叠。我目前正在研究代码,看看是否可以强制每个菜单的宽度固定为 100 x 72。
    • 我想我明白了。如果我更改 widthOffset,我可以显示更多内容 CCScrollLayer *scroller = [[CCScrollLayer alloc] initWithLayers:pagesArray widthOffset: 400]; 它也可以在不需要 avisOnPage 的情况下工作,但我将继续调整和试验,看看这是否有帮助
    猜你喜欢
    • 1970-01-01
    • 2014-01-02
    • 2022-10-04
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 2013-05-13
    • 2013-09-20
    相关资源
    最近更新 更多