How to make CCScrollLayer have multiple avatars per page

753 Views Asked by At

I have a list of about 10 avatars and I am using CCScrollLayer to display paging. Currently, it only shows 1 avatar per page, and I would much prefer if it showed 3 avatars per page but I am unsure of how to do this.

I've attempted to ensure that a new page only generates if there is a MODULUS of 3, but it causes issues because parts of the code needs to be available, such as adding things to the menu.

When I try to use a MODULUS (tied to an if statement) it complains that my menu is out of scope.

My code follows;

// 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];

Screenshot follows. It shows 1 avatar per page.

paging avatars

1

There are 1 best solutions below

9
On BEST ANSWER

seems to me your are seeing what you are programming. Try

// 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];