I am currently developing a card game for the iphone using cocos2d. I am currently in need of a tab view with each tab representing a player and his / her set of cards. Currently i have a single view representing just one player. It seems as though cocos2d is not really built do have multiple views, to do this and it would require some serious amount of hacking around with the code. What would be the most efficient way to accomplish this?
can you spot anything obviously wrong here? I created a new class called PlayerController (from apps delegate) the app delegate calls the scene method, which subsequently populates an array with two "hands" objects and calls initWithPlayerHands (i know it should not be here but i just wanted to get things working first). I have also hard coded moveToPlayerHand to point to element 0
-(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
PlayerController *layer = [PlayerController node];
// add layer as a child to scene
[scene addChild: layer];
HelloWorldLayer *layer1 = [[HelloWorldLayer alloc] init];
HelloWorldLayer *layer2 = [[HelloWorldLayer alloc] init];
allLayers = [[NSMutableArray alloc] initWithCapacity:6];
[allLayers addObject:layer1];
[allLayers addObject:layer2];
[self initWithPlayerHands:allLayers];
// return the scene
return scene;
}
-(id)initWithPlayerHands:(NSMutableArray *)layers
{
NSMutableArray *allPlayers;
if ( (self = [super init]) )
{
currentScreen = 1;
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:NO];
[self setIsTouchEnabled:YES];
scrollWidth = [[CCDirector sharedDirector] winSize].width;
scrollHeight = [[CCDirector sharedDirector] winSize].height;
startWidth = scrollWidth;
startHeight = scrollHeight;
allPlayers = [[NSMutableArray array] retain];
int count = [layers count];
int i = 0;
for (CCLayer *l in layers)
{
l.anchorPoint = ccp(0,0);
l.position = ccp((i*scrollWidth),0);
[self addChild:l ];
i=i+1;
count-=1;
}
totalScreens = i;
}
return self;
}
-(void) moveToPlayerHand:(int)hand //this represents the layer you want to move to
{
float dest = /*((currentScreen-1)*scrollHeight);*/ 0;
id changeHand = [CCEaseBounce actionWithAction:[CCMoveTo actionWithDuration:0.2 position:ccp(0,dest)]];
[self runAction:changeHand];
currentScreen = hand;
}
I have done something similar to this using cocos2d (having different views). I solved the problem by using a scrollIng CCLayer that housed 2 (or more) layers. This scroll layer was part of a scene that had another control layer placed above it (higher z). You can achieve the same thing if you have the different players hands as layers in a scrolling layer and then a separate layer that is higher than the scroller with 2 sprites, or 2 buttons that tell the scrolling layer which of its internal layers to scroll to. The scrolling layer and control layer can communicate using the shared CCScene they are both in.
Sorry, I guess my answer wasn't completely clear. These methods should go in a subclass of a CCLayer, so myScrollerLayer:CCLayer should be the interface. The scene should create the array as well as create the scrollerLayer passing the created array to it. So because you have a reference to both the control layer and the scrollerLayer in the scene you can pass messages to each layer.
Here is the code for the scrolling layer, you should first create your 2 layers that represent the players hand, then you can initiate a new scrolling layer by passing an array of the layers to it:
-(id) initWithPlayerHands:(NSMutableArray *)layers
{
if ( (self = [super init]) )
{
// Make sure the layer accepts touches
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:NO];
[self setIsTouchEnabled:YES];
// Set up the starting variables
currentScreen = 1;
scrollWidth = [[CCDirector sharedDirector] winSize].width;
scrollHeight = [[CCDirector sharedDirector] winSize].height;
startWidth = scrollWidth;
startHeight = scrollHeight;
allPlayers = [[NSMutableArray array] retain]; //iVar that holds the layers that represent the players hands.
// Loop through the array and add the screens
int count = [layers count];
int i = 0;
for (CCLayer *l in layers)
{
l.anchorPoint = ccp(0,0);
l.position = ccp((i*scrollWidth),0);
//Add them with inverse levels so that the touches can pass through all of the board layers (This is something I did special for my project, I don't think you have to)
[self addChild:l z:count];
[allLayers addObject:l];
i=i+1;
count-=1;
}
// Setup a count of the available screens
totalScreens = i;
}
return self;
}
And here is how to move to a players hand:
-(void) moveToPlayerHand:(int)hand //this represents the layer you want to move to
{
id changeHand = [CCEaseBounce actionWithAction:[CCMoveTo actionWithDuration:0.3 position:ccp(-((page-1)*scrollWidth),0)]];
[self runAction:changeHand];
currentScreen = hand;
}
I hope this gets you on the right track, if you want to see how this worked out for me you can check the link in my profile. There is a video in the middle of the page that shows the scrolling.