随机SpriteKit纹理切换语句无法正常工作

时间:2016-02-29 16:11:39

标签: ios objective-c sprite-kit

我目前在Objective C中使用SpriteKit工作,我试图在switch语句中将一些随机纹理设置为9个不同的SKSpriteNodes。目前,我的代码的这一部分如下所示:

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 个答案:

答案 0 :(得分:1)

您的计划流程现在是:

  • 创建随机数
  • 挑选1个纹理
  • 将纹理设置为每个节点

它应该将前两个步骤循环与节点存在的次数相同。尝试类似:

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

}

这只是流程应该如何,请优化代码。祝你好运!