我在创建SKShapeNode时遇到了swift的问题,好像我的' y'坐标是倒置的。
最初我将我的节点放在屏幕的底部并添加了一个物理体(由于我已经将skView.showsPhysics设置为true并验证了这一点,因此正确放置)但每当我尝试与使用UIPanGestureRecognizer的节点除非我与屏幕的上方相对,否则它不会移动。我发现无论节点在哪里都会有反应,但我必须反转输入的Y位置。
此外,如果我在倒置位置向下拖动,节点将向下移动,因此在某种意义上它不会完全反转,因为它仍然会以正确的方向相互作用。
此时我认为它与不同的坐标系有关,但是我无法隔离我的代码的哪一部分导致问题。我在下面提供了我的代码。
override func didMoveToView(view: SKView){
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector("handlePan:"))
self.view?.addGestureRecognizer(gestureRecognizer)
physicsWorld.gravity = CGVector(dx: 0.0, dy: -2.0)
physicsWorld.contactDelegate = self
let userShape = SKShapeNode(rectOfSize: CGSize(width: 30, height: 30))
userShape.position = CGPointMake(self.frame.size.width/2, userShape.frame.size.height/2)
userShape.name = "userShape"
userShape.fillColor = SKColor.blueColor()
userShape.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 30, height: 30))
userShape.physicsBody?.dynamic = true
userShape.physicsBody?.affectedByGravity = false
userShape.physicsBody?.mass = 0.02
userShape.physicsBody?.friction = 0.0
userShape.physicsBody?.restitution = 0.5
addChild(userShape)
}
我的经纪人
func handlePan(recognizer: UIPanGestureRecognizer!)
{
switch recognizer.state{
case .Began:
var touchLocation = recognizer.locationInView(recognizer.view!)
selectNodeForTouch(touchLocation)
case .Changed:
var translation = recognizer.translationInView(recognizer.view!)
translation = CGPointMake(translation.x, translation.y)
self.panForTranslation(translation)
recognizer.setTranslation(CGPointZero, inView: recognizer.view!)
case .Ended:
var velocity = recognizer.velocityInView(recognizer.view)
_selectedNode.physicsBody!.applyImpulse(CGVectorMake(velocity.x * velocityController, velocity.y * velocityController * -1.0))
default:
break;
}
}
func panForTranslation(translation: CGPoint)
{
var position = _selectedNode.position
print(_selectedNode.position)
_selectedNode.position = CGPointMake(position.x + translation.x, position.y - translation.y)
}
func selectNodeForTouch(touchLocation: CGPoint)//NODE SELECTOR
{
var touchedNode = nodeAtPoint(touchLocation)
_selectedNode = touchedNode
}
答案 0 :(得分:0)
我发现它确实与坐标系的某种对话有关。在.begin语句中的handlePan中,我在创建touchLocation变量后直接放置了这一行 “touchLocation = self.convertPointFromView(touchLocation)”,它似乎解决了这个问题。