2016-09-20 4 views
1

iOS 용 (spritekit/swift) 약간의 게임을하고 있습니다.배열에서 노드 제거 spritekit swift

var spritesTree = [SKSpriteNode]() 

가 나는 함수로 배열을 작성하고이 같은 장면에 추가 :

나는했습니다 의 배열은 다음과 같이 정의 내 장면에서을 SKSpriteNode

spritesTree = spritesCollectionTree(count: numTrees) 
    for sprite in spritesTree { 
     addChild(sprite) 
    } 

그런 다음, 상황에 따라 프로세스가 배열에 트리를 더 추가합니다. 나는 인덱스를

  spritesTree[i].removeFromParent() 
      spritesTree.removeAtIndex(i) 

을 알고 있지만 내 문제는 내가 인덱스를 모를 때이 배열에 especific 노드를 제거하는 경우 장면과 배열에서 요소를 제거하는 방법을 알 수 있습니다. 나는 인덱스를 모르는 경우 exemple 들어 스프라이트 중 하나가이 경우

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    /* Called when a touch begins */ 

    for touch in (touches as! Set<UITouch>) { 

     let location = touch.locationInNode(self) 
     let positionInScene = touch.locationInNode(self) 
     let touchedNode = self.nodeAtPoint(positionInScene) 

감동 때, ¿ 어떻게 배열 의 spritesTree SKSpriteNode에서 touchedNode을 제거 할 수 있습니까? 인덱스를 미리 찾기 위해 indexOf에 대한 내용을 읽었지만 내 SKSpriteNode 어레이와 함께 작동하지 않습니다. 제발 도와 주실 수 있나요? 당신은 각 SKSpriteNode의 이름을 만들 수 있습니다

답변

0

Bests : 당신이 노드를 검사 할 때 후

spritesTree[index].name = String(index) 

당신이 답변을

let location = touch.locationInNode(self) 
    let positionInScene = touch.locationInNode(self) 
    let touchedNode = self.nodeAtPoint(positionInScene) 
    var index = Int(touchedNode.name) 
    touchedNode.removeFromParent() 
    spritesTree.removeAtIndex(index) 
+0

감사를 int로 이름을 변환 감동하지만 didn 히 내 문제를 완전히 해결하지 못한다. 위치와 일치하는 다음과 같은 목록 노드 이름 [ "0", "1", "2", "3", "4"]을 상상해보십시오. 이름이 '2'인 첫 번째 항목을 제거하면 모든 항목이 정상적으로 작동하지만 이름이 '4'인 _removeAtIndex_를 시도하면 항목이 0 -> "0", 1 -> "1", 2로 재구성 되었기 때문에 실패합니다 -> "3", 3 -> "4"이고 배열에 "4"위치가 없습니다. 이름에서 항목 위치를 반환하는 함수가 있는지 알고 있습니까? – Xavi

+0

루프에서 삭제 후 모두 이름을 바꿉니다. 또는 다음과 같이 배열에 노드를 찾으려고 할 수 있습니다. 0의 인덱스에 대해 ... spritesTree.count {touchedNode == spritesTree [index] {spritesTree.removeAtIndex (index)}} – gabrielpf