2014-12-15 5 views
4

SKShapeNode를 사용하여 간단하게 선을 그립니다. SpriteKit과 Swift를 사용하고 있습니다. 여기 SKShapeNode를 사용하여 선 그리기

지금까지 내 코드입니다 :

내가 그것을 실행하고 라인 오류가있는 응용 프로그램 충돌 그리려고 할 때마다
var line = SKShapeNode() 
var ref = CGPathCreateMutable() 

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 

    for touch: AnyObject in touches { 
     let location = touch.locationInNode(self) 

    } 
} 

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) { 

    for touch: AnyObject in touches { 
     let locationInScene = touch.locationInNode(self) 

     CGPathMoveToPoint(ref, nil, locationInScene.x, locationInScene.y) 
     CGPathAddLineToPoint(ref, nil, locationInScene.x, locationInScene.y) 
     line.path = ref 
     line.lineWidth = 4 
     line.fillColor = UIColor.redColor() 
     line.strokeColor = UIColor.redColor() 
     self.addChild(line) 

    } 
} 

: 이유는 '이미 부모가있는 SKNode을 추가 Attemped를 : SKShapeNode 이름 : '(null)'누적 프레임 : {{0, 0}, {0, 0}} '

왜 이런 일이 발생합니까?

답변

5

그럼 동일한 하위 인스턴스를 반복해서 추가하고 있습니다. 매번 라인 노드를 생성하고 매번 부모 노드에 추가하면 문제가 해결됩니다.

var ref = CGPathCreateMutable() 

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 

    if let touch = touches.anyObject() as? UITouch { 
     let location = touch.locationInNode(self) 
     CGPathMoveToPoint(ref, nil, location.x, location.y) 
    } 
} 

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) { 

    for touch: AnyObject in touches { 
     let locationInScene = touch.locationInNode(self) 
     var line = SKShapeNode() 
     CGPathAddLineToPoint(ref, nil, locationInScene.x, locationInScene.y) 
     line.path = ref 
     line.lineWidth = 4 
     line.fillColor = UIColor.redColor() 
     line.strokeColor = UIColor.redColor() 
     self.addChild(line) 
    } 
} 
+0

앱이 중단되는 문제를 해결했지만 앱에서 줄을 그어서 표시하지 않았습니다. 내가 원하는 것은 사용자가 손가락을 움직일 때마다 선을 그리는 것입니다. – PoisonedApps

+0

질문에 언급 된 것처럼, 선으로 이동 한 다음 선을 같은 지점으로 그리기 때문에 터치가 시작될 때 추적하십시오. 터치가 움직일 때마다 계속 라인을 생성하십시오. 내 편집을 참조하십시오. – Sandeep

+0

감사합니다. 니가 지금 무슨 뜻인지 이해해. 정말 감사합니다! – PoisonedApps