2012-04-01 61 views
-1

我目前正在爲使用cocos2d的iphone開發一款紙牌遊戲。我目前需要一個選項卡視圖,每個選項卡代表一個玩家和他/她的一套牌。目前我有一個視圖只代表一名球員。看起來好像cocos2d並不是真的有多個視圖,要做到這一點,並且需要對代碼進行大量的黑客攻擊。什麼是最有效的方法來完成這個?cocos2d多玩家選項卡視圖


你能發現任何明顯的錯誤嗎?我創建了一個名爲PlayerController的新類(從應用程序委託) 應用程序委託調用場景方法,隨後使用兩個「手」對象填充數組並調用initWithPlayerHands(我知道它不應該在這裏,但我只想獲取事物首先工作)。我也硬編碼moveToPlayerHand指向元素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; 
} 

回答

0

我已經做了類似這樣的使用cocos2d的(具有不同的視圖)的東西。我通過使用容納2個(或更多)圖層的scrollIng CCLayer解決了這個問題。這個滾動層是一個場景的一部分,其上面放置了另一個控制層(較高的z)。如果您將不同的玩家手分爲滾動圖層中的圖層,然後單獨的圖層高於具有2個精靈的滾動圖標,或者2個指示滾動圖層滾動到哪個內部圖層的按鈕,則可以實現同樣的效果。滾動層和控制層可以使用它們都在共享的CCScene進行通信。

對不起,我想我的答案並不完全清楚。這些方法應該放在CCLayer的子類中,所以myScrollerLayer:CCLayer應該是接口。場景應該創建數組以及創建將創建的數組傳遞給它的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; 

} 

我希望這可以讓你在正確的軌道上,如果你想看看這個摸索出適合我,你可以檢查我的個人資料的鏈接。頁面中間有一個視頻顯示滾動。

+0

感謝您的反應Tams,這可能是我正在尋找的,如果你可以發佈一些代碼,我會非常感激它,非常感謝:) – godzilla 2012-04-01 17:03:56

+0

謝謝你的反應Tams,我給它一個嘗試,但它出現好像我做錯了什麼,雖然我不知道它是什麼。我硬編碼它,所以它總是選擇第一個「子視圖」 - 即我的比賽的第一手。這裏是我的一些代碼,如果你能發現任何明顯的錯誤,我會很感激,如果你可以告知: – godzilla 2012-04-02 01:23:09

+0

我已經編輯我的帖子來清除它,我希望這可以幫助! – tams 2012-04-02 06:32:51