杨锴
2025-03-11 90dc3329d1973fda691e357cf4523d5c7c67fa1d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// SKNodeExtensions.swift - Copyright 2024 SwifterSwift
 
#if canImport(SpriteKit)
import SpriteKit
 
// MARK: - Methods
 
public extension SKNode {
    /// SwifterSwift: Return an array of all SKNode descendants.
    ///
    ///         mySKNode.descendants() -> [childNodeOne, childNodeTwo]
    ///
    func descendants() -> [SKNode] {
        var children = children
        children.append(contentsOf: children.reduce(into: [SKNode]()) { $0.append(contentsOf: $1.descendants()) })
        return children
    }
 
    /// SwifterSwift: The center anchor of the node in its parent's coordinate system.
    ///
    ///         mySKNode.center = CGPoint(x: frame.midX, y: frame.midY)
    ///
    var center: CGPoint {
        get {
            let contents = calculateAccumulatedFrame()
            return CGPoint(x: contents.midX, y: contents.midY)
        }
        set {
            let contents = calculateAccumulatedFrame()
            position = CGPoint(x: newValue.x - contents.midX, y: newValue.y - contents.midY)
        }
    }
 
    /// SwifterSwift: The top left anchor of the node in its parent's coordinate system.
    ///
    ///         mySKNode.topLeft = CGPoint(x: frame.minX, y: frame.maxY)
    ///
    var topLeft: CGPoint {
        get {
            let contents = calculateAccumulatedFrame()
            return CGPoint(x: contents.minX, y: contents.maxY)
        }
        set {
            let contents = calculateAccumulatedFrame()
            position = CGPoint(x: newValue.x - contents.minX, y: newValue.y - contents.maxY)
        }
    }
 
    /// SwifterSwift: The top right anchor of the node in its parent's coordinate system.
    ///
    ///         mySKNode.topRight = CGPoint(x: frame.maxX, y: frame.maxY)
    ///
    var topRight: CGPoint {
        get {
            let contents = calculateAccumulatedFrame()
            return CGPoint(x: contents.maxX, y: contents.maxY)
        }
        set {
            let contents = calculateAccumulatedFrame()
            position = CGPoint(x: newValue.x - contents.maxX, y: newValue.y - contents.maxY)
        }
    }
 
    /// SwifterSwift: The bottom left anchor of the node in its parent's coordinate system.
    ///
    ///         mySKNode.center = GPoint(x: frame.minX, y: frame.minY)
    ///
    var bottomLeft: CGPoint {
        get {
            let contents = calculateAccumulatedFrame()
            return CGPoint(x: contents.minX, y: contents.minY)
        }
        set {
            let contents = calculateAccumulatedFrame()
            position = CGPoint(x: newValue.x - contents.minX, y: newValue.y - contents.minY)
        }
    }
 
    /// SwifterSwift: The bottom right anchor of the node in its parent's coordinate system.
    ///
    ///         mySKNode.bottomRight = CGPoint(x: frame.maxX, y: frame.minY)
    ///
    var bottomRight: CGPoint {
        get {
            let contents = calculateAccumulatedFrame()
            return CGPoint(x: contents.maxX, y: contents.minY)
        }
        set {
            let contents = calculateAccumulatedFrame()
            position = CGPoint(x: newValue.x - contents.maxX, y: newValue.y - contents.minY)
        }
    }
}
 
#endif