当我们尝试运行游戏时,会出现错误。
-(id)initWithSize:(CGSize)size {
self.physicsWorld.contactDelegate = self;
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(0, 72, self.frame.size.width, self.frame.size.height -72)];
self.physicsBody.categoryBitMask = FCBoundaryCategory;
self.physicsBody.contactTestBitMask = FCPlayerCategory;
_gameState = FCGameStateStarting;
_score = 0;
}
答案 0 :(得分:1)
您的方法的返回类型为id
,因此必须返回一个值。您可能在方法结束时错过了行return self;
。
答案 1 :(得分:1)
编译器打印错误,因为您没有返回id
(对象引用),但声明该方法返回id
。但是这种方法还有其他一些问题。
init
方法必须调用super init
方法,将超级调用的结果分配给self
,检查它是否为nil
,然后返回self
}。此外,现代风格是将init方法的返回类型声明为instancetype
。因此:
- (instancetype)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.physicsWorld.contactDelegate = self;
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(0, 72, self.frame.size.width, self.frame.size.height -72)];
self.physicsBody.categoryBitMask = FCBoundaryCategory;
self.physicsBody.contactTestBitMask = FCPlayerCategory;
_gameState = FCGameStateStarting;
_score = 0;
}
return self;
}
了解Cocoa Core Competencies: Initialization中的初始值设定项。