我正在制作一个涉及掉落图像的游戏。我有6个图像,我希望每个图像随机掉落(例如,1美元的钞票将会下降,然后是5美元的钞票(或任何随机生成的图像)落在它旁边或者紧随其后(iOS游戏Sky Burger by:NimbleBit有限责任公司或疯狂的山羊:Barry Calvagna是几个例子))。有谁能够帮我?这是我到目前为止的所有代码
-(void)moneyDrop {
money.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"$1 (bill).png"],
[UIImage imageNamed:@"$5 (bill).png"],
[UIImage imageNamed:@"$20 (bill).png"],
[UIImage imageNamed:@"$50 (bill).png"],
[UIImage imageNamed:@"$100 (bill).png"],nil]; }
我不一定使用掉落的图像,除非你算一个Flappy Bird翻拍或一个Ball Pong游戏,所以请原谅缺乏代码。如果我的问题从未生成,我会重新说出来。
答案 0 :(得分:0)
首先,它听起来像你想要多个UIImageViews,每个UIImageViews都有不同的图像,而不是一个带有animationImages数组的UIImageView。
为了让事情发生变化,您可以使用NSTimer。
- (void)start {
mainTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(mainTimerLoop) userInfo:nil repeats:YES];
}
- (void)mainTimerLoop {
NSLog(@"Loop");
//In here, you can assign random images to and adjust the centers of money image views.
}
您可以制作这样的函数来返回图像。
- (UIImage *)moneyImageForRandomInt:(int)random {
if (random == 0) return [UIImage imageNamed:@"$1 (bill).png"];
if (random == 1) return [UIImage imageNamed:@"$5 (bill).png"];
if (random == 2) return [UIImage imageNamed:@"$20 (bill).png"];
if (random == 3) return [UIImage imageNamed:@"$50 (bill).png"];
return [UIImage imageNamed:@"$100 (bill).png"];
}
使用这样的函数将随机图像分配给UIImageView。
- (void)assignRandomImageToMoneyImageView:(UIImageView *)moneyImageView {
moneyImageView.image = [self moneyImageForRandomInt:arc4random() % 5];
}
无论如何,我就是这样做的。