我在我的游戏项目中有以下代码正常工作,其中一个球随机显示给玩家。
int random = arc4random_uniform(5);
switch (random)
{
case 0:
{
NSLog(@"Case 0 Ball");
}
break;
case 1:
{
NSLog(@"Case 1 Big Ball");
break;
}
case 2:
{
NSLog(@"Case 2 Center Ball");
break;
}
case 3:
{
NSLog(@"Case 3 Red Ball");
break;
}
case 4:
{
NSLog(@"Case 4 Blue Ball");
break;
}
}
我想做的是限制玩家显示的潜在球数,直到玩家达到一定水平。我认为最简单的方法是改变随机案例的数量。例如......
int random = arc4random_uniform(1);
int random = arc4random_uniform(2);
int random = arc4random_uniform(3);
int random = arc4random_uniform(4);
int random = arc4random_uniform(5);
当然问题是当我尝试将这些选项包装在下面的if statement
中时,它将它与Switch分开,这将无效。
int unlockBallLevel = [_levels getCurrentLevel];
if (unlockBallLevel > 5) {
int random = arc4random_uniform(5);
}
我想知道在达到一定水平之前限制球数最合乎逻辑且最有效的方法是什么?
答案 0 :(得分:0)
也许:
switch (arc4random_uniform([_levels getCurrentLevel] < 5 ?: 5))
{
...
}
答案 1 :(得分:0)
我解决了这个问题。我仍然不知道是否有更有效的方法来做到这一点,但是当玩家升级到75级时,以下代码可以工作并解锁更多的球。在75级之后,所有五个球都被解锁。
int unlockAtLevel = [_levels getCurrentLevel];
if (unlockAtLevel >= 1)
{
ballsForRandom = 1;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 10)
{
ballsForRandom = 2;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 30)
{
ballsForRandom = 3;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 50)
{
ballsForRandom = 4;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 75)
{
ballsForRandom = 5;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
int random = arc4random_uniform(ballsForRandom);
switch (random)
{
case 0:
{
NSLog(@"Case 0 Ball");
}
break;
case 1:
{
NSLog(@"Case 1 Big Ball");
break;
}
case 2:
{
NSLog(@"Case 2 Center Ball");
break;
}
case 3:
{
NSLog(@"Case 3 Red Ball");
break;
}
case 4:
{
NSLog(@"Case 4 Blue Ball");
break;
}
}
答案 2 :(得分:0)
根据@ CRD的评论,我对代码进行了以下更改。下面的代码似乎比最后发布的答案更有效,更智能。
int unlockAtLevel = [_levels getCurrentLevel];
if (unlockAtLevel >= 25)
{
ballsForRandom = 6;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 20)
{
ballsForRandom = 5;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 15)
{
ballsForRandom = 4;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 10)
{
ballsForRandom = 3;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 3)
{
ballsForRandom = 2;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 1)
{
ballsForRandom = 1;
[[GCHelper defaultHelper]reportAchievementIdentifier:achievementID1 percentComplete:100];
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
NSLog(@"\n\n###############################################################\n\nCurrently the player is on level %i so there should be %i main balls unlocked!\n\n###############################################################\n\n",unlockAtLevel,ballsForRandom);
int random = arc4random_uniform(ballsForRandom);
switch (random)