如何从SKTextureAtlas制作SKPhysicsBody纹理的动画

时间:2019-01-07 04:05:37

标签: ios swift sprite-kit skphysicsbody sktextureatlas

我有一个SKTextureAtlas,其中有7帧带有透明背景的动画精灵。

我已经可以使用地图集中的单个框架作为物理物体的边界,但是我想知道,如果可能的话,如何让物理物体为地图集中的每个框架更新其轮廓

在这里,我使用称为“ Run0”的框架来创建物理主体,并且该框架已正确应用。我只是想看看是否有可能,或者让物理机构使用图集中从“ Run0”到“ Run7”的每个帧是否完全不切实际。

Image of physics body with the outline of "Run0"

这是我正在处理的代码:

import SpriteKit

GameScene类:SKScene {

let dogSpriteNode = SKSpriteNode(imageNamed: "Run0")
var dogFrames = [SKTexture]()

override func didMove(to view: SKView) {

    physicsWorld.gravity = CGVector(dx: 0, dy: -9.8)
    physicsBody = SKPhysicsBody(edgeLoopFrom: frame)

    dogSpriteNode.setScale(0.1)
    dogSpriteNode.position = CGPoint(x: frame.midX, y: frame.midY)
    addChild(dogSpriteNode)

    let textureAtlas = SKTextureAtlas(named: "Dog Frames")

    for index in 0..<textureAtlas.textureNames.count {
        let textureName = "Run" + String(index)
        dogFrames.append(textureAtlas.textureNamed(textureName))
    }

    dogSpriteNode.physicsBody = SKPhysicsBody(
        texture: textureAtlas.textureNamed("Run0"),
        alphaThreshold: 0.5,
        size: dogSpriteNode.frame.size)

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    dogSpriteNode.run(SKAction.group([SKAction.repeatForever(SKAction.animate(with: dogFrames, timePerFrame: 0.1))]))
    dogSpriteNode.run(SKAction.applyImpulse(CGVector(dx: 0, dy: 1000), duration: 0.1))

}

}

1 个答案:

答案 0 :(得分:0)

实现此目标的一种方法是使用自定义SKAction

extension SKAction {

    public class func animateTexturesWithPhysics(_ frames: [(texture: SKTexture, duration: TimeInterval)], repeatForever: Bool=true) -> SKAction {
        var actions: [SKAction] = []
        for frame in frames {
            // define a custom action for each frame
            let customAction = SKAction.customAction(withDuration: frame.duration) { node, _ in
                // if the action target is a sprite node, apply the texture & physics
                if node is SKSpriteNode {
                    let setTextureGroup = SKAction.group([
                            SKAction.setTexture(frame.texture, resize: false),
                            SKAction.wait(forDuration: frame.duration),
                            SKAction.run {
                                node.physicsBody = SKPhysicsBody(texture: frame.texture, alphaThreshold: 0.5, size: frame.texture.size())
                                // add physics attributes here
                            }
                        ])
                    node.run(setTextureGroup)
                }
            }
            actions.append(customAction)
        }

        // add the repeating action
        if (repeatForever == true) {
            return SKAction.repeatForever(SKAction.sequence(actions))
        }
        return SKAction.sequence(actions)
    }
}

要实现它,您需要创建一个包含帧和持续时间的数组,并将该动作应用于精灵:

typealias Frame = (texture: SKTexture, duration: TimeInterval)
let timePerFrame: TimeInterval = 0.1
let dogFrames: [Frame] = dogTextures.map {
    return ($0, timePerFrame)
}

dogSpriteNode = SKSpriteNode(texture: dogFrames.first)
let dogAnimationAction = SKAction.animateTexturesWithPhysics(dogFrames)
dogSpriteNode.run(dogAnimationAction)