360 degrees spinnable object from a photographed real object

前端 未结 1 1081
不知归路
不知归路 2021-01-07 13:49

I want to create the ability to spin a photographed object 360 degrees.

  • It spins endlessly based on the speed you \"flick\" .
  • You spin it left or rig
1条回答
  •  走了就别回头了
    2021-01-07 14:11

    Here is the second draft of my answer that shows off the basic functionality of a simple sprite animation:

    class GameScene: SKScene {
    
      // Left spin is ascending indices, right spin is descending indices.
      var initialTextures = [SKTexture]()
    
      // Reset then reload this from 0-6 with the correct image sequences from initialTextures:
      var nextTextures = [SKTexture]()
    
      var sprite = SKSpriteNode()
    
      // Use gesture recognizer or other means to set how fast the spin should be.
      var velocity = TimeInterval(0.1)
    
      enum Direction { case left, right }
    
      func spin(direction: Direction, timePerFrame: TimeInterval) {
    
        nextTextures = []
        for _ in 0...6 {
    
          var index = initialTextures.index(of: sprite.texture!)
    
          // Left is ascending, right is descending:
          switch direction {
          case .left:
            if index == (initialTextures.count - 1) { index = 0 } else { index! += 1 }
          case .right:
            if index == 0 { index = (initialTextures.count - 1) } else { index! -= 1 }
          }
    
          let nextTexture = initialTextures[index!]
          nextTextures.append(nextTexture)
          sprite.texture = nextTexture
        }
    
        let action = SKAction.repeatForever(.animate(with: nextTextures, timePerFrame: timePerFrame))
        sprite.run(action)
      }
    
      override func didMove(to view: SKView) {
        removeAllChildren()
    
        // Make our textures for spinning:
        for i in 0...6 {
          initialTextures.append(SKTexture(imageNamed: "img_\(i)"))
        }
        nextTextures = initialTextures
    
        sprite.texture = nextTextures.first!
        sprite.size = nextTextures.first!.size()
        addChild(sprite)
        spin(direction: .left, timePerFrame: 0.10)
      }
    
      override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        spin(direction: .right, timePerFrame: velocity)
      }
    
      override func touchesEnded(_ touches: Set, with event: UIEvent?) {
        spin(direction: .left, timePerFrame: velocity)
      }
    }
    

    Right now you just click / release to alternate right left.

    Todo for next draft:
    - Implement gesture recognizer for velocity
    - Implement decay if wanted (so it will slow down over time)

    (Old video, new code does not reset frame to 0):

    Image assets are found here for the animation: https://drive.google.com/open?id=0B3OoSBYuhlkgaGRtbERfbHVWb28

    0 讨论(0)
提交回复
热议问题