2016-10-28 11 views
0

누구나 내가이 같은 노드에 충동을 적용하려고 할 때 왜 말할 수 있습니까?사용자가 스 와이프하여 SKSpriteNode에 충동을 적용하십시오.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    let touch:UITouch = touches.first! as UITouch 
    startPoint = touch.location(in: view) 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    defer { 
     startPoint = nil 
    } 
    guard touches.count == 1, let startPoint = startPoint else { 
     return 
    } 
    if let touch = touches.first { 
     let endPoint = touch.location(in: view) 
     //Calculate your vector from the delta and what not here 
     direction = CGVector(dx: endPoint.x - startPoint.x, dy: endPoint.y - startPoint.y) 

     L1Ball.physicsBody?.applyImpulse(CGVector(dx: direction.dx.hashValue, dy: direction.dy.hashValue)) 
     } 
} 

나는 hashValue를 사용하는 것과 관련이 있다고 생각합니다. 나는 CGVector 방향에서 값을 얻는 또 다른 방법을 모른다. 방향 벡터를 임펄스 벡터로 사용하려고하면 충돌합니다. 어떤 통찰력? 감사!

답변

1

direction.dx 및 direction.dy에서 .hashValue를 제거했을 때 코드가 작동했습니다 (해시 값은 comparisons에 사용됨). 아, 볼 노드가 사라지지 않습니다. 해시 값은 충분히 크므로 볼에 힘을 적용하면 볼의 속도가 커서 다음 프레임이 렌더링 될 때까지 볼이 화면에서 벗어날 정도로 커집니다.

+0

네, 괜찮습니다. hashValue를 잘못 사용했습니다. 나는 이것과 스프라이트가 더 현실적으로 움직 이도록 편집했습니다. 벡터 크기를 줄이기 위해 필요합니다. ? (: impulseDirectionX, DY : impulseDirectionY) CGVector (DX) self.L1Ball.physicsBody impulseDirectionX = (direction.dx/4) 하게 impulseDirectionY = (-direction.dy/4) self.L1Ball.physicsBody .applyImpulse하자 ? .affectedByGravity = true – Lyres

+0

좋습니다. 나는 당신의 질문에 정확하게 대답 했습니까? – Ralph

+0

정말 고마워! – Lyres