Objective c 使用未声明的标识符“;更新;
当我运行我正在使用的应用程序时,只发生一个错误:Objective c 使用未声明的标识符“;更新;,objective-c,Objective C,当我运行我正在使用的应用程序时,只发生一个错误: - (void)update { NSTimeInterval secondsSinceLastDraw = -([self.lastUpdateTime timeIntervalSinceNow]); self.pacmanYVelocity = self.pacmanYVelocity - (self.acceleration.x * secondsSinceLastDraw); self.p
- (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"];
}
}
}
使用未声明的标识符“更新”
以下是发生错误的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"];
}
}
}
问题是在
update
方法中有movePacman
方法。在其他方法中不能有方法。将它们分开:
- (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"];
}
谢谢你回答我。对不起,我是开发新手,如何将它们分开?谢谢我可以礼貌地建议你找一本关于Objective-C编程语言的好教程吗。现在花在学习语言基础知识上的时间(在尝试编写下一个畅销游戏之前)将为您节省大量的时间。祝你好运