我是目标c的新手,我已经创建了一个应用程序,其中蜜蜂将会掉落,熊会试图抓住它们,一切正常。现在我想限制蜜蜂数量下降到20或者什么,有人可以帮我怎么做吗?
我不确定是将循环定时器放入循环还是循环放置蜜蜂方法,其中的任何输入都将非常有用。
beeArray = [[NSMutableArray alloc]init];
timer1 = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(dropBee) userInfo:nil repeats:YES];
-(void)dropBee
{
CGRect screenRect = [[UIScreen mainScreen]bounds];
CGFloat screenWidth = screenRect.size.width;
// CGFloat screenHeight = screenRect.size.height;
UIImageView *beeImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bee-honey.png"]];
int resultX = (int)roundf(screenWidth);
int randomX = 0 + rand()%(resultX -0);
[beeArray addObject:beeImage];
beeImage.frame = CGRectMake(50, -10, 50, 50);
beeImage.center = CGPointMake(randomX, 50);
[self.view addSubview:beeImage];
[UIView beginAnimations:@"beemovement" context:NULL];
[UIView setAnimationDuration:2];
beeImage.center = CGPointMake(randomX, 600);
[UIView commitAnimations];
}
答案 0 :(得分:0)
您如何在应用程序中添加蜜蜂?看一些代码会很有用。如果您的方法类似于-(void)addABee
,那么您可以创建一个变量来计算蜜蜂的数量。
在您的标头文件中:
@interface SomeController : UIViewController {
int _maxNumberOfBees;
}
然后在您的实施中:
-(void)theMethodYouUseToCreateYourBeeArray {
beeArray = [[NSMutableArray alloc]init];
timer1 = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(dropBee) userInfo:nil repeats:YES];
_maxNumberOfBees = 20;
// etc...
}
-(void)dropBee {
if ([beeArray count] >= _maxNumberOfBees) {
// if the number of bees is greater than or equal to _maxNumberOfBees...
NSLog(@"Too many bees!");
} else {
// add your bee to the scene
CGRect screenRect = [[UIScreen mainScreen]bounds];
CGFloat screenWidth = screenRect.size.width;
// etc etc etc...
}
}
希望有所帮助。