如何使CAScrollLayer每页有多个头像

时间:2012-02-04 17:57:10

标签: cocos2d-iphone scroll custom-paging ccscrolllayer

我有一个大约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个头像。

paging avatars

1 个答案:

答案 0 :(得分: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];