控制随机生成元素的数量

时间:2011-04-30 13:22:36

标签: 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);
    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];

}

1 个答案:

答案 0 :(得分:0)

您可能需要调整触发计时器的频率。这是一个例子,只需更改“2.0”所在的代码:

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