-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"play"]){
NSLog(@"play was touched");
SKScene *mainGameScene = [[MainGame alloc] initWithSize:self.size];
if (!mainGameScene){
SKTransition *transition = [SKTransition fadeWithColor:[UIColor blackColor] duration:0.5];
[self.view presentScene:mainGameScene transition:transition];
}
}
從我的理解上面的檢查的代碼,如果mainGameScene
是零,如果它再通過if語句去。
這樣做有益嗎?還是隻是浪費了代碼?因爲如果多次調用此代碼的方法不會創建新對象SKScene
?
如果你在'if(!mainGameScene)'之前分配/ init'mainGameScene',那麼......看起來if塊永遠不會是真的,因此不會執行。 – staticVoidMan
如何在alloc/init之前測試一個對象? – Mutch95