为SKShapeNode制作SKPhysicsBody

时间:2019-04-18 17:43:35

标签: swift sprite-kit uibezierpath

我正在尝试制作一款游戏,使玩家在移动时以某种方式在地面上滑动而不会掉落在斜坡上。我创建了一个在地面上的SKShapeNode中转换的BezierPath,播放器是SKSpriteNode。我已经在Apple网站上找到了使用此类的方法:https://developer.apple.com/documentation/spritekit/skphysicsjointsliding 问题在于,它需要第一个主体的SKPhysicsBody。 我似乎无法将ShapeNode设置为PhysicsBody。

    //this is where I create my ground shape
    let groundBezier = UIBezierPath()
    groundBezier.move(to: .zero)
    groundBezier.addLine(to: CGPoint(x: 0, y: 1000))
    groundBezier.addLine(to: CGPoint(x: 500, y: 1000))
    groundBezier.addQuadCurve(to : CGPoint(x: 750, y: 750),
                          controlPoint: CGPoint(x: 650, y: 1000))
    groundBezier.addQuadCurve(to : CGPoint(x: 1000, y: 500),
                          controlPoint: CGPoint(x: 850, y: 500))
    groundBezier.addCurve(to : CGPoint(x: 1400, y: 700),
                      controlPoint1: CGPoint(x: 1200, y: 500),
                      controlPoint2: CGPoint(x: 1200, y: 700))
    groundBezier.addCurve(to : CGPoint(x: 1700, y: 400),
                          controlPoint1: CGPoint(x: 1600, y: 700),
                          controlPoint2: CGPoint(x: 1600, y: 400))
    groundBezier.addLine(to: CGPoint(x: 1700, y: 0))
    groundBezier.addLine(to: .zero)
    // Convert First Slope From UIBezierPath to SKShapeNode
    let groundNode = SKShapeNode(path: groundBezier.cgPath)
    // Define First Slope
    groundNode.lineWidth = 5
    groundNode.physicsBody = SKPhysicsBody(edgeChainFrom:   groundNode.path!)
    groundNode.physicsBody?.restitution = 0.0
    groundNode.physicsBody?.isDynamic = false
    groundNode.fillColor = UIColor.white


    // this is where I create my players physics body I Followed apple docs
    // Create Player's Circular Physics Body
    let circularPlayertexture = SKSpriteNode(texture: playertexture)
    // Define Player's Circular Physics Body
    circularPlayertexture.physicsBody = SKPhysicsBody(circleOfRadius: max(circularPlayertexture.size.width / 2,
                                                                          circularPlayertexture.size.height / 2))
    // Make Player's Final Physics Body
    player.physicsBody = SKPhysicsBody(texture: playertexture,
                                       size: CGSize(width: circularPlayertexture.size.width,
                                                    height: circularPlayertexture.size.height))

我希望能够在我的播放器有点粘在地面上的情况下移动。 你知道如何解决这个问题吗? 您知道其他方法吗?

0 个答案:

没有答案