SKSpriteNode frame is off

I am trying to create an open circle from UIBezierPath and turn it into an SKShapeNode, which will later be converted to an SKSpriteNode.

I had a problem where I could not figure out how to compress Sprite, while the line width did not decrease. You can see the solution here: Resize sprite without reducing content

I ended up fixing some things and creating a new class to implement custom SKShapeNode using this function:

class ShrinkableShape: SKShapeNode {

let level: Int
let bezierPath: UIBezierPath
let scale: CGFloat
let finalLineWidth: CGFloat
let from: SKSpriteNode

let initialLineWidth = CGFloat(20)
let duration = 0.3

// level would be from 1 to 5 (it is in a for loop)
init(level: Int, from: SKSpriteNode) {
    self.level = level
    self.from = from

    // create the open circle  which is (43 * level) + 140
    bezierPath = UIBezierPath(arcCenter: CGPoint(x: 0, y: 0), radius: CGFloat(((43 * level) + 140)), startAngle: CGFloat(GLKMathDegreesToRadians(-50)), endAngle: CGFloat(M_PI * 2), clockwise: false)

    // calls static function in this class so it is more readable 
    scale = ShrinkableShape.scaleFrom(level: level) / ShrinkableShape.scaleFrom(level: level + 1)
    finalLineWidth = (initialLineWidth / scale)

    // inits shape and then sets it up to specific values
    super.init()
    setup()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setup() {
    // set sprite to path, set color, set line width and rotate it 
    self.path = bezierPath.cgPath
    self.strokeColor = UIColor(red:0.98, green:0.99, blue:0.99, alpha:1.00)
    self.lineWidth = 20
    self.position = from.position

    // FOR DEBUG: you will see what this makes in the screenshot
    let color = SKShapeNode(rect: self.frame)
    color.fillColor = UIColor.red
    color.alpha = 0.2
    color.position = self.position
    self.addChild(color)
}

// Makes the radius from the value given (only 1...5) 183, 226, 269, etc
static func scaleFrom(level: Int) -> CGFloat {
    return (CGFloat((level * 43) + 140) * 2.0)
}

}

enter image description here If you look at the screenshot, you may notice that the white circle is slightly different from where it should be aligned (the gray bar and the white circle should be on top of each other). This only happens when I call the scale function shown in the solution (linked above).

, - , . , ( , , )

? ? SKShapeNode? , ?

- Edit-- . - , SKShapeNode. , setup(). , , .

+1
1

, , , , /, "" .

, , Red - , , - , . , /, .

enter image description here

+1

Source: https://habr.com/ru/post/1657763/


All Articles