【问题标题】:How to get current texture from current frame of animation in SKAction?如何从 SKAction 中的当前动画帧获取当前纹理?
【发布时间】:2017-06-28 03:46:05
【问题描述】:

我正在尝试为旋转/从左到右的东西制作动画,但每当我打电话时 spinLeft() 或 spinRight() 那么动画总是从第 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)
}

【问题讨论】:

    标签: swift animation sprite-kit


    【解决方案1】:

    你可以这样做(语法可能有点不对,但你明白了):

    这里的关键是 .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()
            }
        }
    }
    

    如果您使用滑动手势识别器之类的东西,您可以使用它的方向来设置 isUp Bool 值和该滑动的速度为 changeImage 函数的 amountOfTime。

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

    希望这会有所帮助!

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-06
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    相关资源
    最近更新 更多