2013-11-25 2 views
4

안녕하세요, 저는 spriteKit과 objective-c에서 새로운데, 메서드에서 spriteNode를 만들고 다른 메서드 (동일한 .m 파일)에서 제거하려고합니다. 이 메서드에서는 스프라이트 :spritekit에서 자식 노드에 액세스

(void)createSceneContents{ /*in this method i create and add the spaceship spriteNode 
     SKSpriteNode *spaceship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"]; 

     //code... 

     //Add Node 
     [self addChild:spaceship]; 
    } 

그리고 지금 내가 그것을 만져 노드를 제거하고 싶지만, 난 단지 핸들 터치 이벤트에 대한 방법을 알고있다 :

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

내가에서 내 우주선 노드에 액세스하려고 해요 거기에서 나는 할 수 없다. 나는 성공하지 않고 모든 것을 시도했다. 메서드에서 노드를 다른 노드로 보내는 방법이 있습니까 ?? 또는 그것을 보내지 않고 자식 노드에 선언 된 메서드에서 자식 노드에 액세스 할 수 있습니까? ?

답변

8

이 시도

[sprite setName:@"NodeName"]; 

을 그리고 당신은 이름으로 액세스 할 수 있습니다 :

[self childNodeWithName:@"NodeName"]; 
+0

이있는'<''<@ "노드 이름"'오타 또는 구문 나는 익숙하지 않은가? –

+0

@MartinThompson 내가 편집했는데, 오타였습니다. 가리켜 주셔서 감사합니다. – Greg

2

예, 방법이 있습니다.

모든 SKNode 객체에는 "name"이라는 속성 (NSString 클래스)이 있습니다. 그리고이 이름을 사용하여 노드의 자식을 열거 할 수 있습니다.

이 시도 :

spaceship.name = @"spaceship"; 

[self enumerateChildNodesWithName:@"spaceship" usingBlock:^(SKNode *node, BOOL *stop) { 
    [node removeFromParent]; 
}]; 

touchesBegan에하지만 조심,이 그것의 부모로부터 이름이 "우주선"로 모든 노드를 제거합니다. 제거 프로세스에 대해 더 자세히 선택하려면 SKSpriteNode의 하위 클래스를 사용하여 제거해야하는지 여부를 파악하는 데 사용할 수있는 값을 보유하거나 일부 논리를 기반으로 배열에 스프라이트를 추가 한 다음 배열을 사용하여 제거하십시오.

행운을 빈다. 노드의

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint positionInScene = [touch locationInNode:self]; 
    SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene]; 
} 

또한 설정할 수 있습니다 이름 :

0

은 "이름"을 사용하여 속성이 작동하지만 해당 노드의 인스턴스가 하나만 있다고 생각되면 속성을 만들어야합니다. touchesBegan에서 다음

self.spaceship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"]; 

[self addChild:self.spaceship]; 

: 당신이 그것을 추가 할 때

@property (strong, nonatomic) SKSpriteNode *spaceship; 

그런 다음

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = touches.anyObject; 
    SKSpriteNode *node = [self nodeAtPoint:[touch locationInNode:self]]; 
    if (node == self.spaceship) { 
     [self.spaceship removeFromParent]; 
    } 
} 
관련 문제