Swift 如何在SKAction中从动画的当前帧获取当前纹理?

Swift 如何在SKAction中从动画的当前帧获取当前纹理?,swift,animation,sprite-kit,Swift,Animation,Sprite Kit,我正在尝试设置从左向右旋转的动画,但是每当我调用 然后动画始终从第0帧开始 换句话说,我想说旋转10帧中的4帧,然后停下来 从第4帧开始,以相反方向旋转。现在,它重置为第0帧 var textures = [SKTexture]() // Loaded with 10 images later on. var sprite = SKSpriteNode() func spinLeft() { let action = SKAction.repeatForever(.animate(with

我正在尝试设置从左向右旋转的动画,但是每当我调用 然后动画始终从第0帧开始

换句话说,我想说旋转10帧中的4帧,然后停下来 从第4帧开始,以相反方向旋转。现在,它重置为第0帧

var textures = [SKTexture]() // Loaded with 10 images later on.
var sprite = SKSpriteNode()

func spinLeft() {
  let action = SKAction.repeatForever(.animate(with: textures, timePerFrame: 0.1))
  sprite.run(action)
}

func spinRight() {
  let action = SKAction.repeatForever(.animate(with: textures, timePerFrame: 0.1)).reversed()
  sprite.run(action)
}
您可以这样做(语法可能有点错误,但您明白了要点):

这里的键是.index(of:…),它将获得索引

func spinUp() {
    let index = textures.index(of: sprite.texture)
    if index == textures.count - 1 {
        sprite.texture = textures[0]
    }
    else {
        sprite.texture =  textures[index + 1]   
    }
}

func spinDown() {
    let index = textures.index(of: sprite.texture)
    if index == 0 {
        sprite.texture = textures[textures.count - 1]
    }
    else {
        sprite.texture =  textures[index - 1]   
    }
}

func changeImage(_ isUp: Bool, _ amountOfTime: CGFloat) {
    let wait = SKAction.wait(duration: amountOfTime)
    if isUp {
        run(wait) {
            self.imageUp()
        }
    }
    else {
        run(wait) {
            self.imageDown()
        }
    }
}
如果您使用类似于滑动手势识别器的工具,则可以使用其方向为changeImage函数的AmountOffTime设置isUp Bool值和滑动速度

changeImage只会更改图像一次,因此您需要在其他地方处理此问题,或者创建另一个函数(如果您希望它持续旋转或最终消失)


希望这有帮助

这太棒了!以下是我对您的实现的版本: