2011-04-30 86 views
0

我正在生成隨機火球,但我的問題是它產生大量的火球。控制隨機生成元素的數量

如何控制生成的火球數量?

- (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]; 

} 

回答

0

您可能需要調整您的定時器發射頻率。下面是一個例子,只需更改「2.0」以下的代碼即可:

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

它正在工作!我已經設置了時間間隔1.0,然後重複:YES。結果我得到了幾個從屏幕頂部落下的火球,這就是我想要的!謝謝!! – user720235 2011-05-02 05:06:31