首页 文章

设计圆形以外的物理实体

提问于
浏览
4

我正在使用SpriteKit进行我的第一场比赛,以及我一直在使用的敌人外星人(SKSpriteNode子类)物理机构:

self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width/2)

到目前为止,大多数外星人都是圆形的 . 但是,如果我有椭圆形或方形外星人,那么设计物理机构的最佳方法是什么?我尝试了这个方形:

let squarePath = UIBezierPath(rect: CGRect(x: -64, y: -64, width: 128, height: 128))
self.physicsBody = SKPhysicsBody(polygonFromPath: squarePath.CGPath)

但我不知道这是否是最有效的,也不知道如何精确制作省略号 . 任何建议都会很棒!

2 回答

  • 2

    使用路径是创建复杂物理体的唯一方法,但是,您可以使用SKPhysicsBody创建矩形(rectangleOf:CGSize(width:100,height:100))

    编辑:使用此选项可帮助从图像创建有效路径:http://insyncapp.net/SKPhysicsBodyPathGenerator.html

    注意:这只会生成图像的路径,您需要通过删除路径和连接点来清理它自己 .

  • 1

    让SpriteKit为你画出形状!

    如果你有一个精灵,你需要为它创建一个 PhysicsBody ,让SpriteKit为你做 . SpriteKit可以分析纹理,找到边框(使用alpha /透明通道),最后创建一个与图像匹配的物理体 .

    假设我将这个精灵添加到我的场景中

    let croc = SKSpriteNode(imageNamed: "croc_walk01")
    croc.position = CGPoint(x: frame.midX, y: frame.midY)
    addChild(croc)
    

    enter image description here

    自动创建PhysicsBody形状

    现在我只需添加此行

    croc.physicsBody = SKPhysicsBody(texture: croc.texture!, size: croc.texture!.size())
    

    SpriteKit会自动为我创建PhysicsBody .

    enter image description here

    你能看到精灵周围的直线吗?而已 . 让我隐藏精灵,这样你就可以更好地看到它

    croc.hidden = true
    

    enter image description here

相关问题