gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:11:23 24 4
gpt4 key购买 nike

我目前正在 Objective C 中的 SpriteKit 中工作,我正在尝试在 switch 语句中将多个随机纹理设置为 9 个不同的 SKSpriteNode。目前,我的这部分代码如下所示:

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 节点都显示相同的随机纹理。但是,我希望每个 Sprite 节点的纹理是随机的,每次选择不同的节点。

类似于:

  • 从 switch 语句中选择随机纹理
  • 添加到第一个 Sprite 节点
  • 从 switch 语句中选择随机纹理
  • 添加到第二个 Sprite 节点
  • 等等

有人能帮忙吗?

最佳答案

你的程序现在的流程是:

  • 创建随机数
  • 选择 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;
}

}

这只是一个大概的流程,请优化代码。祝你好运!

关于ios - 随机 SpriteKit 纹理切换语句无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35704198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com