我用 SKSpritenode
做到了 func AddTopTriangels() {
TopTriangel = SKSpriteNode(imageNamed:"Top-Triangels.png")
TopTriangel.position = CGPointMake(frame.size.width/2, frame.size.height/2 + 370)
TopTriangel.size = CGSize.init(width: 440, height: 35)
addChild(TopTriangel)
}
但我想用 SKShapenode 创建它,我该怎么做?
答案 0 :(得分:1)
您应该像在问题中一样创建SKSpriteNode
,并将物理身体贴在三角形上:
var trianglePath = CGPathCreateMutable()
CGPathMoveToPoint(trianglePath, nil, -TopTriangel.size.width/2, -TopTriangel.size.height/2)
CGPathAddLineToPoint(trianglePath, nil, TopTriangel.size.width/2, -TopTriangel.size.height/2)
CGPathAddLineToPoint(trianglePath, nil, 0, TopTriangel.size.height/2)
CGPathAddLineToPoint(trianglePath, nil, -TopTriangel.size.width/2, -TopTriangel.size.height/2)
TopTriangel.physicsBody = SKPhysicsBody(polygonFromPath: trianglePath)
您可以添加以下这一行:
sceneView.showsPhysics = true
要确保形状与您的图像匹配(SKSpriteNode
),请记住在调试后将其删除。