2016-02-29 93 views
0

我目前在Objective-C中的SpriteKit中工作,並且我試圖在一個switch語句中將一些隨機紋理設置爲9個不同的SKSpriteNodes。目前,我的這段代碼如下所示:隨機SpriteKit紋理開關語句無法正常工作

int randomCard = arc4random_uniform(13); 

switch (randomCard) 
{ 
    case 0: 
     staveCard = [SKTexture textureWithImageNamed:@"Card_C.png"]; 
     break; 

    case 1: 
     staveCard = [SKTexture textureWithImageNamed:@"Card_D.png"];   
     break; 

    case 2: 
     staveCard = [SKTexture textureWithImageNamed:@"Card_E.png"]; 
     break; 

    case 3: 
     staveCard = [SKTexture textureWithImageNamed:@"Card_F.png"]; 
     break; 

    case 4: 
     staveCard = [SKTexture textureWithImageNamed:@"Card_G.png"]; 
     break; 

    case 5: 
     staveCard = [SKTexture textureWithImageNamed:@"Card_A.png"]; 
     break; 

    case 6: 
     staveCard = [SKTexture textureWithImageNamed:@"Card_B.png"]; 
     break; 

    case 7: 
     staveCard = [SKTexture textureWithImageNamed:@"Card_C2.png"]; 
     break; 

    case 8: 
     staveCard = [SKTexture textureWithImageNamed:@"Card_D2.png"]; 
     break; 

    case 9: 
     staveCard = [SKTexture textureWithImageNamed:@"Card_E2.png"];     
     break; 

    case 10: 
     staveCard = [SKTexture textureWithImageNamed:@"Card_F2.png"]; 
     break; 

    case 11: 
     staveCard = [SKTexture textureWithImageNamed:@"Card_G2.png"]; 
     break; 

    case 12: 
     staveCard = [SKTexture textureWithImageNamed:@"Card_A2.png"]; 
     break; 
} 

faceDownStaveCard1 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
faceDownStaveCard2 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
faceDownStaveCard3 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
faceDownStaveCard4 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
faceDownStaveCard5 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
faceDownStaveCard6 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
faceDownStaveCard7 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
faceDownStaveCard8 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
faceDownStaveCard9 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 

當我在模擬器上運行它時,所有sprite節點都顯示相同的隨機紋理。不過,我希望紋理對於每個精靈節點都是隨機的,每次選取一個不同的節點。

是這樣的:

  • 挑隨機紋理從switch語句
  • 添加到第一子畫面節點
  • 挑隨機紋理從switch語句
  • 添加到第二子畫面節點

是否有人能夠幫助無線這個?

回答

1

你的程序的流程現在是:

  • 創建隨機數
  • 選1個質地
  • 設置紋理到每個節點

它應該循環的第一2個步驟相同量時間作爲節點存在。嘗試像這樣:

for (int i = 0; i<9; i++) { 
    int randomCard = arc4random_uniform(13); 
    switch (randomCard) 
    { 
     case 0: 
      staveCard = [SKTexture textureWithImageNamed:@"Card_C.png"]; 
      break; 

     case 1: 
      staveCard = [SKTexture textureWithImageNamed:@"Card_D.png"];   
      break; 

     case 2: 
      staveCard = [SKTexture textureWithImageNamed:@"Card_E.png"]; 
      break; 

     case 3: 
      staveCard = [SKTexture textureWithImageNamed:@"Card_F.png"]; 
      break; 

     case 4: 
      staveCard = [SKTexture textureWithImageNamed:@"Card_G.png"]; 
      break; 

     case 5: 
      staveCard = [SKTexture textureWithImageNamed:@"Card_A.png"]; 
      break; 

     case 6: 
      staveCard = [SKTexture textureWithImageNamed:@"Card_B.png"]; 
      break; 

     case 7: 
      staveCard = [SKTexture textureWithImageNamed:@"Card_C2.png"]; 
      break; 

     case 8: 
      staveCard = [SKTexture textureWithImageNamed:@"Card_D2.png"]; 
      break; 

     case 9: 
      staveCard = [SKTexture textureWithImageNamed:@"Card_E2.png"];     
      break; 

     case 10: 
      staveCard = [SKTexture textureWithImageNamed:@"Card_F2.png"]; 
      break; 

     case 11: 
      staveCard = [SKTexture textureWithImageNamed:@"Card_G2.png"]; 
      break; 

     case 12: 
      staveCard = [SKTexture textureWithImageNamed:@"Card_A2.png"]; 
      break; 
    } 

    switch (i) 
    { 
     case 0: 
      faceDownStaveCard1 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
      break; 

     case 1: 
      faceDownStaveCard2 = [SKSpriteNode spriteNodeWithTexture:staveCard];   
      break; 

     case 2: 
      faceDownStaveCard3 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
      break; 

     case 3: 
      faceDownStaveCard4 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
      break; 

     case 4: 
      faceDownStaveCard5 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
      break; 

     case 5: 
      faceDownStaveCard6 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
      break; 

     case 6: 
      faceDownStaveCard7 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
      break; 

     case 7: 
      faceDownStaveCard8 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
      break; 

     case 8: 
      faceDownStaveCard9 = [SKSpriteNode spriteNodeWithTexture:staveCard]; 
      break; 
    } 

} 

這只是一個流程應該如何的想法,請優化代碼。祝你好運!

+0

這完全爲我工作,非常感謝你!我將來會記住這一點! –

+1

哈哈,不用擔心,無論如何,嘗試以更好的方式重新編寫此代碼 –

+1

我還建議使用數組而不是開關盒。 – EmilioPelaez