我目前正在使用cocos2d开发iphone的纸牌游戏。我目前需要一个标签视图,每个标签代表一个玩家和他/她的一组牌。目前我只有一个代表一个玩家的视图。似乎cocos2d并没有真正构建,确实有多个视图,要做到这一点,它需要对代码进行大量的黑客攻击。什么是最有效的方法来实现这一目标?
-(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;
}
答案 0 :(得分:0)
我使用cocos2d(具有不同的视图)做了类似的事情。我通过使用容纳2层(或更多层)的scrollIng CCLayer解决了这个问题。此滚动图层是场景的一部分,其上方有另一个控制图层(较高的z)。如果您将不同的玩家手作为滚动图层中的图层然后使用高于具有2个精灵的滚动条的单独图层,或者通过2个按钮告诉滚动图层滚动到哪个内部图层,则可以实现相同的操作。滚动层和控制层可以使用它们都在的共享CCScene进行通信。
对不起,我想我的回答并不完全清楚。这些方法应该放在CCLayer的子类中,因此myScrollerLayer:CCLayer应该是接口。场景应该创建数组,并创建scrolllerLayer将创建的数组传递给它。因此,您可以在场景中引用控制层和scrollerLayer,您可以将消息传递给每个层。
以下是滚动图层的代码,首先应创建代表玩家手的2个图层,然后通过将图层数组传递给它来启动新的滚动图层:
-(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;
}
以下是如何转移到球员手中:
-(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;
}
我希望这能让你走上正轨,如果你想看看这对我有什么影响,你可以查看我个人资料中的链接。页面中间有一个显示滚动的视频。