Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 控制随机生成元素的数量_Iphone_Objective C_Random - Fatal编程技术网

Iphone 控制随机生成元素的数量

Iphone 控制随机生成元素的数量,iphone,objective-c,random,Iphone,Objective C,Random,我正在生成随机火球,但我的问题是它生成的火球数量太多 如何控制生成的火球数量 - (void)onTimer { // build a view from our fire image UIImageView* fireView = [[UIImageView alloc] initWithImage:fireballs]; int startX = round(random() % 320); int endX = round(random() % 320)

我正在生成随机火球,但我的问题是它生成的火球数量太多

如何控制生成的火球数量

- (void)onTimer
{
    // build a view from our fire image
    UIImageView* fireView = [[UIImageView alloc] initWithImage:fireballs];


    int startX = round(random() % 320);
    int endX = round(random() % 320);
    double scale = 1 / round(random() % 100) + 1.0;
    double speed = 1 / round(random() % 100) + 1.0;


    fireView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);



    [self.view addSubview:fireView];

    [UIView beginAnimations:nil context:fireView];

    [UIView setAnimationDuration:5 * speed];


    fireView.frame = CGRectMake(endX, 500.0, 25.0 * scale, 25.0 * scale);

    [UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];

}

您可能需要调整启动计时器的频率。下面是一个示例,只需更改代码,其中“2.0”如下所示:

[NSTimer scheduledTimerWithTimeInterval:2.0
    target:self
    selector:@selector(targetMethod:)
    userInfo:nil
    repeats:NO];

它开始工作了!!我已将时间间隔设置为1.0,并重复:是。结果我得到了从屏幕顶部落下的几个火球,这就是我想要的!!谢谢