Embedding anchorPoint in a subclass of SKNode

I have my own class inheriting from SKNode and would like to implement SKSpriteNode functionality with an anchor in this class. In particular, I am trying to achieve a centralized user node in the parent node automatically (anchorPoint by 0.5, 0.5) without having to offset it by half its width / height.

Any tips?

Change . I ended up creating an anchorPoint variable first and then overriding the position variable and setting the superposition changed by the anchor.

var anchorPoint = CGPoint(x: 0.5, y: 0.5)

override var position: CGPoint {
    set {
        super.position = CGPoint(x: self.position.x - self.size.width * self.anchorPoint.x, y: self.position.y - self.size.height * self.anchorPoint.y)
    }
    get {
        return super.position
    }
}
+4
source share
2 answers

setPosition . anchorPoint.

, 100x100. (.5,.5). - , , . , (0,0), (100 *.5,100 *.5) = (50,50). (0,0) (50,50).

, .

+3

"" SKNode SKNode, , , anchorPoint . , .

var anchorPoint: CGPoint = CGPoint( x: 0.5, y: 0.5 )
{
    didSet
    {
        let translateX = ( anchorPoint.x - 0.5 ) * controlSize.width
        let translateY = ( anchorPoint.y - 0.5 ) * controlSize.height
        rootNode!.position = CGPoint(x: translateX, y: translateY)
    }
}
0

All Articles