Cocoa 为什么我的N定时器只有一个工作?

Cocoa 为什么我的N定时器只有一个工作?,cocoa,animation,nstimer,Cocoa,Animation,Nstimer,我不明白为什么只有animateDarkAst动画可以工作。前两个计时器(一个在processKeys上运行,另一个在animateDarkAst上运行)工作正常,但其他计时器工作不正常。不管我写定时器的顺序是什么,只有这两种方法与各自的定时器一起工作。对于其他3个动画,屏幕上不会显示任何内容,因为它们的方法(animateLightAst、animateSmallAst、animateComet)中没有处理任何代码 我应该如何处理这件事?我不能同时有5个计时器吗?正确的问题是:“为什么至少有一

我不明白为什么只有animateDarkAst动画可以工作。前两个计时器(一个在processKeys上运行,另一个在animateDarkAst上运行)工作正常,但其他计时器工作不正常。不管我写定时器的顺序是什么,只有这两种方法与各自的定时器一起工作。对于其他3个动画,屏幕上不会显示任何内容,因为它们的方法(animateLightAst、animateSmallAst、animateComet)中没有处理任何代码

我应该如何处理这件事?我不能同时有5个计时器吗?

正确的问题是:“为什么至少有一个计时器工作?”

正如您在中所看到的,方法的签名必须采用一个参数(将消息发送给代理的
NSTimer
实例)

A选择定时器触发时发送给目标的消息

选择器应具有以下签名:timerFireMethod: (包括一个冒号,指示该方法采用参数)。这个 计时器将自身作为参数传递,因此该方法将采用 以下模式:


添加计时器时更改方法和选择器的签名。

谢谢@Amin。我照你说的做了,但我认为问题在于我在时间间隔上加的随机双精度。你知道如何用重复的NSTimer随机分配每个时间间隔吗?我不理解这个新的(?)问题。请问一个全新的问题,并链接到这里。不管我解决了它。不过,我还有一个问题。对于同时在屏幕上显示大量动画NSImageViews(在本例中为子弹图像),我是否最好在核心动画中使用层进行动画制作,而不是按照我的方式进行动画制作?当我用太空船射击时,我注意到了一个巨大的减速,因为我必须创建一个新的NSImageView,并在每次击中空格键射击子弹时设置帧等。
    // Prepare asteroids.
    _asteroiddark = [[NSImageView alloc] init];
    [_asteroiddark setImage: [NSImage imageNamed:@"asteroiddark"]];
    [_asteroiddark setFrame: theModel.darkAstRect];

    _asteroidlight = [[NSImageView alloc] init];
    [_asteroidlight setImage: [NSImage imageNamed:@"asteroidwhite"]];
    [_asteroidlight setFrame: theModel.lightAstRect];

    _asteroidsmall = [[NSImageView alloc] init];
    [_asteroidsmall setImage: [NSImage imageNamed:@"asteroidsmall"]];
    [_asteroidsmall setFrame: theModel.smallAstRect];

    // ... and comets
    _comet = [[NSImageView alloc] init];
    [_comet setImage:[NSImage imageNamed:@"comet"]];
    [_comet setFrame: theModel.cometRect];




    // Set up key Processing timer for fluid spaceship movement.
    timer1 = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(processKeys) userInfo:nil repeats:YES];


    // Make a random value for first animation timer.
    double randomInterval1 = arc4random() % (4 - 1) + 1;


    // Set up key Processing timer for animation.
    timer2 = [NSTimer scheduledTimerWithTimeInterval:randomInterval1 target:self selector:@selector(animateDarkAst) userInfo:nil repeats:YES];

   double randomInterval2 = ((double)arc4random() / 3) * (3 - 1) + 1;


     timer3 = [NSTimer scheduledTimerWithTimeInterval:randomInterval2 target:self selector:@selector(animateSmallAst) userInfo:nil repeats:YES];


    double randomInterval3 = ((double)arc4random() / 5) * (5 - 1) + 1;


    timer4 = [NSTimer scheduledTimerWithTimeInterval:randomInterval3 target:self selector:@selector(animateLightAst) userInfo:nil repeats:YES];




    double randomInterval4 = ((double)arc4random() / 6) * (6 - 1) + 1;


    timer5 = [NSTimer scheduledTimerWithTimeInterval:randomInterval4 target:self selector:@selector(animateComet) userInfo:nil repeats:YES];




}
return self;
}



-(void) animateDarkAst
{
int randX = arc4random_uniform(self.bounds.size.width);

int randSize = 40 + arc4random() % (120-40+1);

CGPoint startPoint = CGPointMake(randX, self.bounds.size.height);


[_asteroiddark setFrame: NSMakeRect(startPoint.x, startPoint.y, randSize, randSize)];
[self addSubview:_asteroiddark];

// Create animation (down y-axis)
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
    [context setDuration:1.5];

    [context setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    _asteroiddark.animator.frame = CGRectOffset(_asteroiddark.frame, 0, -self.bounds.size.height - 180);
} completionHandler:nil];
}


-(void) animateSmallAst
{

int randX = arc4random_uniform(self.bounds.size.width);

int randSize = 40 + arc4random() % (120-40+1);

CGPoint startPoint = CGPointMake(randX, self.bounds.size.height);


[_asteroidsmall setFrame: NSMakeRect(startPoint.x, startPoint.y, randSize, randSize)];
[self addSubview:_asteroidsmall];

// Create animation (down y-axis)
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
    [context setDuration:1.5];

    [context setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    _asteroidsmall.animator.frame = CGRectOffset(_asteroidsmall.frame, 0, -self.bounds.size.height - 180);
} completionHandler:nil];
}

-(void) animateLightAst
 {
int randX = arc4random_uniform(self.bounds.size.width);

int randSize = 40 + arc4random() % (120-40+1);

CGPoint startPoint = CGPointMake(randX, self.bounds.size.height);


[_asteroidlight setFrame: NSMakeRect(startPoint.x, startPoint.y, randSize, randSize)];
[self addSubview:_asteroidlight];

// Create animation (down y-axis)
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
    [context setDuration:1.5];

    [context setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    _asteroidlight.animator.frame = CGRectOffset(_asteroidlight.frame, 0, -self.bounds.size.height - 180);
} completionHandler:nil];
}

-(void) animateComet
{
int randX = arc4random_uniform(self.bounds.size.width);

int randSize = 40 + arc4random() % (120-40+1);

CGPoint startPoint = CGPointMake(randX, self.bounds.size.height);


[_comet setFrame: NSMakeRect(startPoint.x, startPoint.y, randSize, randSize)];
[self addSubview:_comet];

// Create animation (down y-axis)
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
    [context setDuration:1.5];

    [context setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    _comet.animator.frame = CGRectOffset(_comet.frame, 0, -self.bounds.size.height - 180);
} completionHandler:nil];
}