2014-04-12 3 views
1
-(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의 새 개체를 만들지 않으므로이 방법이 유용할까요 아니면 단지 코드 낭비입니까?

+1

'if (! mainGameScene)'직전에/init'mainGameScene'을 할당하면 if 블록은 결코 사실이 아니므로 처음부터 실행되지 않습니다. – staticVoidMan

+0

alloc/init 전에 객체를 테스트하려면 어떻게해야합니까? – Mutch95

답변

1

nil 인 경우 presentScene이 아니기 때문에 mainGameScene이 초기화되었는지 확인하는 데 사용됩니다. 당신이 개체와 뭔가를하려고으로 객체가, 다음,

SKScene *mainGameScene; 
//creates new variable - place it at the beginning of the code, before @implementation and @interface 
if(mainGameScene){ 
// it is already inited (you have already tapped once), it will be called always, since it is inited at else 
//it is called when you tap the second, the third time, etcetera 
SKTransition *transition = [SKTransition fadeWithColor:[UIColor blackColor] duration:0.5]; 
[self.view presentScene:mainGameScene transition:transition]; 
}else 
{ 
//Calls only once, when you first touching the screen, and initing mainGameScene. 
//It won't be called ANYMORE, since it is inited. 
mainGameScene = [[MainGame alloc] initWithSize:self.size]; 
} 

지금이 코드는 쓸모가 초기화되어 있는지 확인 만하려는 경우 , 그 초기화되지 않았습니다.
필자의 경우 mainGameScene은 일단 inited되면 다시 초기화되지 않습니다.

+0

이렇게 여러 번 호출되면 매번 새로운'mainGameScene'을 할당하지 않을 것입니까? @AlieN – Mutch95

+0

init 메소드는 장면이 초기화 될 때까지 호출됩니다. 이미 초기화 된 경우 다시 호출되지 않으므로 매번 새로운 mainGameScene을 만들지 않습니다. – AlieN

+0

코드를 업데이트했는데 지금 무슨 뜻인지 이해하겠습니까? – Mutch95