使用未声明的标识符“更新”

时间:2014-02-15 03:41:21

标签: objective-c

当我运行应用程序时,我只会发生一个错误:

  

使用未声明的标识符“update”

以下是发生错误的ViewController.m中的代码:

- (void)update {
        NSTimeInterval secondsSinceLastDraw = -([self.lastUpdateTime timeIntervalSinceNow]);

        self.pacmanYVelocity = self.pacmanYVelocity - (self.acceleration.x * secondsSinceLastDraw);
        self.pacmanXVelocity = self.pacmanXVelocity - (self.acceleration.y * secondsSinceLastDraw);

        CGFloat xDelta = secondsSinceLastDraw * self.pacmanXVelocity * 500;
        CGFloat yDelta = secondsSinceLastDraw * self.pacmanYVelocity * 500;

        self.currentPoint = CGPointMake(self.currentPoint.x + xDelta,
                                        self.currentPoint.y + yDelta);

        [self movePacman];

        self.lastUpdateTime = [NSDate date];

        - (void)movePacman {
            self.previousPoint = self.currentPoint;

            CGRect frame = self.pacman.frame;
            frame.origin.x = self.currentPoint.x;
            frame.origin.y = self.currentPoint.y;

            self.pacman.frame = frame;

            // Rotate the sprite
            CGFloat newAngle = (self.pacmanXVelocity + self.pacmanYVelocity) * M_PI * 4;
            self.angle += newAngle * kUpdateInterval;

            CABasicAnimation *rotate;
            rotate                     = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
            rotate.fromValue           = [NSNumber numberWithFloat:0];
            rotate.toValue             = [NSNumber numberWithFloat:self.angle];
            rotate.duration            = kUpdateInterval;
            rotate.repeatCount         = 1;
            rotate.removedOnCompletion = NO;
            rotate.fillMode            = kCAFillModeForwards;
            [self.pacman.layer addAnimation:rotate forKey:@"10"];
        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题是您的movePacman方法中包含update方法。你不能在其他方法中使用方法。将它们分开:

- (void)update {
    NSTimeInterval secondsSinceLastDraw = -([self.lastUpdateTime timeIntervalSinceNow]);

    self.pacmanYVelocity = self.pacmanYVelocity - (self.acceleration.x * secondsSinceLastDraw);
    self.pacmanXVelocity = self.pacmanXVelocity - (self.acceleration.y * secondsSinceLastDraw);

    CGFloat xDelta = secondsSinceLastDraw * self.pacmanXVelocity * 500;
    CGFloat yDelta = secondsSinceLastDraw * self.pacmanYVelocity * 500;

    self.currentPoint = CGPointMake(self.currentPoint.x + xDelta,
                                    self.currentPoint.y + yDelta);

    [self movePacman];

    self.lastUpdateTime = [NSDate date];
}

- (void)movePacman {
    self.previousPoint = self.currentPoint;

    CGRect frame = self.pacman.frame;
    frame.origin.x = self.currentPoint.x;
    frame.origin.y = self.currentPoint.y;

    self.pacman.frame = frame;

    // Rotate the sprite
    CGFloat newAngle = (self.pacmanXVelocity + self.pacmanYVelocity) * M_PI * 4;
    self.angle += newAngle * kUpdateInterval;

    CABasicAnimation *rotate;
    rotate                     = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotate.fromValue           = [NSNumber numberWithFloat:0];
    rotate.toValue             = [NSNumber numberWithFloat:self.angle];
    rotate.duration            = kUpdateInterval;
    rotate.repeatCount         = 1;
    rotate.removedOnCompletion = NO;
    rotate.fillMode            = kCAFillModeForwards;
    [self.pacman.layer addAnimation:rotate forKey:@"10"];
}