在我最初的构建中,我的精灵是静态的,所以为了使碰撞物理尽可能精确,而不是使用rectangleOfSize来传递SKPhysicsBody,我使用这个工具来定义路径SpriteKit's SKPhysicsBody with polygon helper tool。
然而,现在我正在动画精灵,以便他们在游戏中来回移动(显然,我的物理路径在上面是静态的),所以我的身体不再匹配玩家在屏幕上看到的东西。
这个辅助工具看起来像是苹果最终会在API中修复的一些黑客,最近SpriteKit中有没有什么东西可以帮助我在这里通过node,定义一个精确的物理体,而不是硬编码的方法?如果没有,还有其他选择吗?
发布于 2016-10-05 19:01:07
不确定这是什么表现,但您可以为每个纹理预先生成物理体,然后使用自定义操作将雪碧与其physicsBody一起动画化。
下面是一个示例:
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)
}https://stackoverflow.com/questions/39879706
复制相似问题