spritekit中嵌套的SKNodes?

时间:2016-12-28 13:53:29

标签: ios sprite-kit swift3 sknode

我想制作一个空心框,其中间有一个浮动数字。盒子和里面的数字应该根据物理特性表现出来,好像数字被弹簧连接到所有4个角(或两侧)一样。在我的代码中,这根本不会发生什么。相反,物体分崩离析。这个定位看起来也不对。

import SpriteKit

public class BoxNode: SKNode {

    public func setup(size: CGSize, number: Int) {


        let shape = SKShapeNode()
        shape.path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: size.width, height: size.height)).cgPath
        shape.position = CGPoint(x: 0, y: 0)
        shape.fillColor = UIColor.red
        shape.strokeColor = UIColor.blue
        let lineWidth = size.width * 0.05
        shape.lineWidth = lineWidth
        //edge loop of the inside of the path.
        //this idea is probably wrong
        //TODO- figure out how this physicsBody ought to be set up.

        shape.physicsBody = SKPhysicsBody(rectangleOf: shape.frame.size)
//        shape.physicsBody = SKPhysicsBody(edgeLoopFrom: (UIBezierPath(rect: CGRect(x: lineWidth, y: lineWidth, width: size.width - (2 * lineWidth), height: size.height - 2 * lineWidth)).cgPath))
//        
        addChild(shape)
        let numberLabel = SKLabelNode()
        numberLabel.fontName = "Chalkduster"
        numberLabel.text = String(number)
        numberLabel.position = CGPoint(x: size.width / 2, y: size.height / 2)
        numberLabel.verticalAlignmentMode = .center
        numberLabel.horizontalAlignmentMode = .center
        numberLabel.physicsBody = SKPhysicsBody(rectangleOf: numberLabel.frame.size, center: numberLabel.position)
        shape.addChild(numberLabel)
        let spring = SKPhysicsJointSpring()

        guard let bodyA = shape.physicsBody else { return }
        spring.bodyA = bodyA

        guard let bodyB = numberLabel.physicsBody else { return }
        spring.bodyB = bodyB
    }
}

0 个答案:

没有答案
相关问题