0

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

寻呼头像

4

1 回答 1

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];
于 2012-02-04T18:32:23.007 回答