Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios cocos2d多播放器选项卡视图_Ios_Cocoa Touch_Cocos2d Iphone - Fatal编程技术网

Ios cocos2d多播放器选项卡视图

Ios cocos2d多播放器选项卡视图,ios,cocoa-touch,cocos2d-iphone,Ios,Cocoa Touch,Cocos2d Iphone,我目前正在使用cocos2d为iphone开发一款纸牌游戏。我目前需要一个选项卡视图,每个选项卡代表一个玩家和他/她的牌集。目前我有一个单一的视图,只代表一个玩家。似乎cocos2d并不是真正构建的,但它确实有多个视图,要做到这一点,需要对代码进行大量的修改。实现这一目标最有效的方法是什么 你能发现这里有什么明显的错误吗?我创建了一个名为PlayerController的新类(来自apps delegate) app委托调用scene方法,该方法随后用两个“手”对象填充数组,并调用initWi

我目前正在使用cocos2d为iphone开发一款纸牌游戏。我目前需要一个选项卡视图,每个选项卡代表一个玩家和他/她的牌集。目前我有一个单一的视图,只代表一个玩家。似乎cocos2d并不是真正构建的,但它确实有多个视图,要做到这一点,需要对代码进行大量的修改。实现这一目标最有效的方法是什么


你能发现这里有什么明显的错误吗?我创建了一个名为PlayerController的新类(来自apps delegate) app委托调用scene方法,该方法随后用两个“手”对象填充数组,并调用initWithPlayerHands(我知道它不应该在这里,但我只是想先让它工作)。我还硬编码了movetoplayer并指向元素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;   
}

我使用cocos2d做了类似的事情(有不同的视图)。我通过使用包含2层(或更多)的滚动CCLayer解决了这个问题。该滚动层是场景的一部分,该场景上放置了另一个控制层(更高的z)。如果您将不同的玩家手作为滚动层中的层,然后是一个比滚动器更高的单独层,并带有两个精灵,或者两个按钮,告诉滚动层要滚动到哪个内部层,则可以实现相同的效果。滚动层和控制层可以使用它们所处的共享场景进行通信

对不起,我想我的答案不是很清楚。这些方法应该放在CCLayer的子类中,因此myScrollerLayer:CCLayer应该是接口。场景应该创建数组以及将创建的数组传递给它的滚动层。因此,由于您对场景中的控制层和滚动层都有引用,因此可以将消息传递到每个层

下面是滚动层的代码,您应该首先创建两个代表玩家手的层,然后通过向其传递一个层数组来启动新的滚动层:

-(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;

}

我希望这能让你走上正轨,如果你想看看这对我来说是怎样的,你可以查看我个人资料中的链接。在页面中间有一个视频,显示滚动。

谢谢TAMS的响应,这很可能是我正在寻找的,如果你能发布一些代码,我会非常感激的,多谢:谢谢TAMS的响应,我试了一下,但似乎我做错了什么,虽然我不知道是什么。我硬编码它,所以它总是选择第一个“子视图”-即我的游戏的第一手。这是我的一些代码,如果你能发现任何明显的错误,如果你能提出建议,我会非常感激:谢谢Tams,我花了一整晚的时间,但我的框架几乎都在工作,只需处理触摸,使每一层不与其他层冲突:)将触摸传递到正确的场景有问题,你有没有遇到同样的问题?你应该只有一个场景,多个层(代表玩家的手)存储在场景中的一个数组中。将此数组传递给(子类)scrollingLayer,并在场景中保留对此的引用。如果要拥有控制层,可以将其添加到场景中。因此,在单个场景中,您应该为scrollingLayer和control layer调用[self-addChild:]。每个玩家手牌层都可以声明自己为self.isTouchEnabled=YES。而且滚动层不应该吞下触摸。如果要实现UIS手势,最好是在场景中。