【问题标题】:How to rotate sprite in sprite kit with swift如何快速旋转精灵套件中的精灵
【发布时间】:2014-06-16 08:18:38
【问题描述】:

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

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

演示问题的视频: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))
            }     
        }
    }

【问题讨论】:

  • 所以看看我是否理解正确:您希望圆圈开始按初始触摸指示的方向旋转。在此之后您只想能够切换方向,而不是完全停止它?
  • 这正是我想要的
  • 我想在触摸左半边屏幕时向左旋转,如果我不触摸停止并且如果我触摸右半边屏幕向右旋转
  • 对不起,除了旋转还要移动,还是说要根据触摸的位置触发旋转?
  • 如果我触摸屏幕的左半部分向左旋转如果停止触摸左停止旋转,则不动。如果我触摸屏幕的右半部分向右旋转,如果停止触摸右停止旋转,则不动。

标签: ios rotation sprite-kit swift ios8


【解决方案1】:

好的,拿三个。我不能 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")
        }
      }
    

编辑:更改示例以满足问题中的特定需求。代码格式有些奇怪,不太清楚那里发生了什么。

【讨论】:

  • 是的,你是最好的,但我想在触摸屏幕左半边旋转时移动,如果我不触摸停止并且如果我触摸屏幕右半边旋转向右
  • 如果我触摸屏幕的左半部分向左旋转如果停止触摸左停止旋转,则不动。如果我触摸屏幕的右半部分向右旋转,如果停止触摸右停止旋转,则不动。
  • 不太确定您要达到的目的:如果您在同一半屏幕上再次触摸,您是否希望停止旋转?还是您希望在触摸另一侧时停止旋转?
  • 如果我从屏幕上抬起手指,旋转应该结束
  • OK,那你需要去掉else if currentRotationDirection == newRotationDirection,实现currentRotationDirection = .none,调用touchesEnded中的stopRotation()函数
【解决方案2】:

我建议在添加新操作之前删除所有当前操作,因为它们可能会发生冲突。我仍然不确定您要达到的目标,您能解释一下吗?

【讨论】:

    【解决方案3】:

    您是否厌倦了删除 touchesEnded 处的操作?在我希望我的对象根据用户触摸的屏幕的哪一半从左向右移动之前,我遇到了类似的问题。我有一个类似的问题,如果我再次向右和向右触摸,我的速度会加倍,如果我向左触摸,我的对象就会停止。

    我通过添加 touchesEnded 方法解决了这个问题,并在我放手时删除了这些操作。这基本上重置了我的移动动作,因此当我放手时对象会停止并立即向相反方向移动,而不必点击它两次。

    【讨论】:

      猜你喜欢
      • 2013-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多