我需要在Swift中制作类似重力点的东西。 SpriteNode位于屏幕中间,通过左手势或右手势,它可以沿x轴移动。
但是之后a' RightSwipe'它应该回到他的起点。你可以说它应该看起来像是沿着x轴的跳跃。
世界引力不可能是一个特定的点,所以我怎么能强迫'它回到他原来的观点?
我尝试使用SKFieldNode的RadialGravityField,但在节点冲动完成后,它会回到原来的点并消失。 我怎么能避免这种情况?
我的代码:
{
.......
Node = SKSpriteNode(texture: Node1)
Node.size = CGSize(width: 100, height: 140)
Node.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 4)
Node.zPosition = 3
Node.runAction(RunAnimation)
Node.physicsBody = SKPhysicsBody(rectangleOfSize: Node.size)
Node.physicsBody?.dynamic = true
Node.physicsBody?.allowsRotation = false
Node.physicsBody?.usesPreciseCollisionDetection = true
Node.physicsBody?.restitution = 0
Node.physicsBody?.velocity = CGVectorMake(0, 0)
self.addChild(Node)
let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("SwipeRight:"))
swipeRight.direction = .Right
view.addGestureRecognizer(swipeRight)
let swipeLeft:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("SwipeLeft:"))
swipeLeft.direction = .Left
view.addGestureRecognizer(swipeLeft)
}
func SwipeRight(recognizer:UISwipeGestureRecognizer) {
Node.physicsBody?.applyImpulse(CGVectorMake(10,0))
}
func SwipeLeft(recognizer:UISwipeGestureRecognizer) {
Node.physicsBody?.applyImpulse(CGVectorMake(-10,0))
}
override func update(currentTime: CFTimeInterval) {
if(Node.position.x > self.frame.size.width / 1.6)
{
fieldNode = SKFieldNode.radialGravityField()
fieldNode.enabled = true
fieldNode.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 4)
fieldNode.strength = 0.4
addChild(fieldNode)
}
if(Node.position.x < self.frame.size.width / 2.6)
{
fieldNode = SKFieldNode.radialGravityField()
fieldNode.enabled = true
fieldNode.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 4)
fieldNode.strength = 0.4
addChild(fieldNode)
}