我有一个不遵循 randomPoint 方法的 NSMutableArray(命名为硬币)。当我进入 iOS 模拟器时,硬币没有出现(因为没有图像),它们停留在屏幕的中心,而不是随机分配的点。这就是制作新硬币的方法:
if ([coins count] == 0){
CGSize windowSize = [CCDirector sharedDirector].winSize;
CGPoint randomPointOnScreen = ccp((float)(arc4random() % 100) / 100 * windowSize.width, (float)(arc4random() % 100) / 100 * windowSize.height);
randomCoinType = arc4random() % kNumberOfCoinTypes + 1; // I have this because my files are named like "coin.1.png" and such
kNumberOfCoinTypes 被定义为 10 给我一个数字 1-10。这是应该制造硬币的相应空隙:
-(void)createCoinsAt:(CGPoint)position withCointype:(int)type{
NSString *imageFile;
switch (type) {
case 1:
imageFile = @"coin.1.png";
break;
case 2:
imageFile = @"coin.2.png";
break;
case 9:
case 3:
imageFile = @"coin.3.png";
break;
case 7:
case 8:
default:
case 4:
imageFile = @"coin.4.png";
break;
case 5:
imageFile = @"coin.5.png";
break;
case 6:
imageFile = @"coin.6.png";
break;
}
Coins *c = [Coins spriteWithFile:imageFile];
//Coins *c = [Coins spriteWithFile:[NSString stringWithFormat:@"coin.%d.png", arc4random() % 5 + 1 ]];
c.type = type;
c.position = position;
c.velocity = ccp(0,0);
[coins addObject:c];
[self addChild:c z:2];
}
我在一个命令之后有多个案例,因为我不知道如何使一个数字比另一个数字出现得更频繁。