首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在动画中动态计算SKPhysicsBody?

如何在动画中动态计算SKPhysicsBody?
EN

Stack Overflow用户
提问于 2016-10-05 16:53:04
回答 1查看 111关注 0票数 1

在我最初的构建中,我的精灵是静态的,所以为了使碰撞物理尽可能精确,而不是使用rectangleOfSize来传递SKPhysicsBody,我使用这个工具来定义路径SpriteKit's SKPhysicsBody with polygon helper tool

然而,现在我正在动画精灵,以便他们在游戏中来回移动(显然,我的物理路径在上面是静态的),所以我的身体不再匹配玩家在屏幕上看到的东西。

这个辅助工具看起来像是苹果最终会在API中修复的一些黑客,最近SpriteKit中有没有什么东西可以帮助我在这里通过node,定义一个精确的物理体,而不是硬编码的方法?如果没有,还有其他选择吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-05 19:01:07

不确定这是什么表现,但您可以为每个纹理预先生成物理体,然后使用自定义操作将雪碧与其physicsBody一起动画化。

下面是一个示例:

代码语言:javascript
复制
func testAnimation() {

    var frameTextures = [SKTexture]()
    var physicsBodies = [SKPhysicsBody]()

    for index in 1...8 {
        // The animation has 8 frames
        let texture = SKTexture(imageNamed: "guy\(index)")
        frameTextures.append(texture)
        let physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
        physicsBody.affectedByGravity = false
        physicsBodies.append(physicsBody)
    }

    let sprite = SKSpriteNode(texture: frameTextures[0])
    let framesPerSecond = 16.0
    let animation = SKAction.customAction(withDuration: 1.0, actionBlock: { node, time in
        let index = Int((framesPerSecond * Double(time))) % frameTextures.count
        if let spriteNode = node as? SKSpriteNode {
            spriteNode.texture = frameTextures[index]
            spriteNode.physicsBody = physicsBodies[index]
        }
    })

    sprite.run(SKAction.repeatForever(animation));
    sprite.position = CGPoint(x: 0, y: 0)

    addChild(sprite)

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39879706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档