如何在sprite套件中用swift旋转sprite

嗨,我的问题是关于两个精灵的旋转。 当我触摸屏幕的右半边时,旋转开始并移动sprite2和sprite3。 如果我触摸屏幕的左半边,则旋转停止,因为速度= 0。如果再次触摸左半部分,则开始旋转。

但是,如果触摸与当前旋转方向相对应的屏幕的一半,则速度被复制。 我希望能够改变旋转的方向,但速度要保持不变。

video显示问题:http: //youtu.be/HxLwl1QZiNM

import SpriteKit class GameScene: SKScene { let sprite = SKSpriteNode(imageNamed:"bWhite") let sprite2 = SKSpriteNode(imageNamed:"bBlue") let sprite3 = SKSpriteNode(imageNamed:"bRed") let circle = SKSpriteNode(imageNamed:"bCircle") override func didMoveToView(view: SKView) { /* Setup your scene here */ backColor = SKColor(red: 0, green: 0, blue: 0, alpha: 1) self.backgroundColor = backColor sprite.setScale(1.25) sprite2.setScale(1) sprite3.setScale(1) sprite.position = CGPointMake(self.frame.size.width/2, (self.frame.size.height/2)-200); circle.position = CGPointMake(self.frame.size.width/2, (self.frame.size.height/2)-200); sprite3.zRotation = 172.77 sprite2.anchorPoint = CGPointMake(-1.10, 0.5); sprite3.anchorPoint = CGPointMake(-1.10, 0.5); self.addChild(sprite) self.addChild(circle) sprite.addChild(sprite2) sprite.addChild(sprite3) } override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { /* Called when a touch begins */ for touch: AnyObject in touches { let location = touch.locationInNode(self) let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1) let action2 = SKAction.rotateByAngle(CGFloat(-M_PI), duration:1) if (location.x > self.frame.size.width / 2) { sprite2.runAction(SKAction.repeatActionForever(action2)) sprite3.runAction(SKAction.repeatActionForever(action2)) } else { sprite2.runAction(SKAction.repeatActionForever(action)) sprite3.runAction(SKAction.repeatActionForever(action)) } } } 

好的,拿三个。 我不是100%确定轮换何时结束的具体细节,但是所有的部分应该在下面的代码中。 它会:

  • 根据第一次触摸的位置开始顺时针或逆时针旋转
  • 如果用户再次触摸相同的方向,则停止旋转
  • 如果用户触摸屏幕的另一半,则切换旋转

      import SpriteKit enum rotationDirection{ case clockwise case counterClockwise case none } class GameScene: SKScene { var currentRotationDirection = rotationDirection.none let sprite = SKSpriteNode(color: UIColor.yellowColor(), size: CGSizeMake(100, 100)) override func didMoveToView(view: SKView) { sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size) sprite.physicsBody.affectedByGravity = false sprite.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)) self.addChild(sprite) } override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { let touch : UITouch = touches.anyObject() as UITouch let touchPosition = touch.locationInNode(self) let newRotationDirection : rotationDirection = touchPosition.x < CGRectGetMidX(self.frame) ? .clockwise : .counterClockwise if currentRotationDirection != newRotationDirection && currentRotationDirection != .none{ reverseRotation() currentRotationDirection = newRotationDirection } else if currentRotationDirection == newRotationDirection{ stopRotation() currentRotationDirection = .none } else if (currentRotationDirection == .none){ setupRotationWith(direction: newRotationDirection) currentRotationDirection = newRotationDirection } } func reverseRotation(){ let oldRotateAction = sprite.actionForKey("rotate") let newRotateAction = SKAction.reversedAction(oldRotateAction) sprite.runAction(newRotateAction(), withKey: "rotate") } func stopRotation(){ sprite.removeActionForKey("rotate") } func setupRotationWith(#direction: rotationDirection){ let angle : Float = (direction == .clockwise) ? Float(M_PI) : -Float(M_PI) let rotate = SKAction.rotateByAngle(angle, duration: 1) let repeatAction = SKAction.repeatActionForever(rotate) sprite.runAction(repeatAction, withKey: "rotate") } } 

编辑:改变例子以迎合问题中的特定需求。 有些奇怪的代码格式不知道发生了什么。

我build议在添加新的之前删除所有当前操作,因为它们可能会冲突。 我还不确定你想达到什么目的,你能解释一下吗?

你只是累touchesEnded删除行动? 我之前有一个类似的问题,我想根据用户触摸屏幕的哪一部分从左到右移动对象。 我有一个类似的问题,如果我再次触及右侧,我的速度会翻倍,如果我碰到了,我的物体就会停止。

我通过添加touchesEnded方法解决了这个问题,并且只要我放开,就删除了这个操作。 这基本上重置了我的移动动作,所以当我放开的时候对象会停下来,马上走向相反的方向,而不必两次敲击它。