首页 > 解决方案 > 如何将一组skspritenodes一起移动

问题描述

我想将一组 skspritenodes 一起移动。我已经把它们放在同一个名字下。但我想将它们作为一个整体向左移动。我怎样才能做到这一点?每个节点都有不同的 x 和 y 位置,那么我可以做些什么来改变节点的位置,使它们都保持在一起但横向移动?

标签: swiftsprite-kit

解决方案


您可以通过搜索将 x 或 y 添加到所有节点

self.enumerateChildNodes(withName: "//NodeName", using: { node, stop in
        if let node = node as? SKSpriteNode {
            //move
         }
})

在此处输入图像描述

或将它们添加到向量中:

class TTESTGameScene: SKScene {
    
    var allBoxes: [SKSpriteNode] = []
    
    override func didMove(to view: SKView) {
        physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
        view.allowsTransparency = true
        self.backgroundColor = .clear
        view.alpha = 1.0
        view.isOpaque = true
        view.backgroundColor = SKColor.clear.withAlphaComponent(0.0)
        
        let upButton = SKShapeNode(rect: CGRect(x: 0, y: view.frame.maxY - 40, width: 66, height: 33), cornerRadius: 20)
        upButton.fillColor = .yellow
        upButton.name = "upButton"
        addChild(upButton)
        let downButton = SKShapeNode(rect: CGRect(x: 80, y: view.frame.maxY - 40, width: 66, height: 33), cornerRadius: 20)
        downButton.fillColor = .orange
        downButton.name = "downButton"
        addChild(downButton)
        let addBox = SKShapeNode(rect: CGRect(x: 200, y: view.frame.maxY - 40, width: 66, height: 33), cornerRadius: 20)
        addBox.fillColor = .red
        addBox.name = "addBox"
        addChild(addBox)
    }

    func addBox() {
        let box = SKSpriteNode(color: SKColor.red, size: CGSize(width: 10, height: 10))
        box.position = CGPoint(x: CGFloat.random(in: 10..<200), y: CGFloat.random(in: 10..<200))
        box.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 10, height: 10))
        box.physicsBody?.affectedByGravity = false
        addChild(box)
        allBoxes.append(box)
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else { return }
        let location = touch.location(in: self.view)
        
        let sceneTouchPoint = self.convertPoint(fromView: location)
        let touchedNode = self.atPoint(sceneTouchPoint)
        print(touchedNode.name)
        if touchedNode.name == "upButton" {
            allBoxes.forEach() { box in
                box.position.addY(10)
            }
        }
        else if touchedNode.name == "downButton" {
            allBoxes.forEach() { box in
                box.position.addY(-10)
            }
        }
        else if touchedNode.name == "addBox" {
            addBox()
        }
            
        
    }
}
extension CGPoint {
    public mutating func addX(_ x:CGFloat){
        let p:CGPoint = CGPoint(x: self.x + x, y: self.y)
        self = p
    }
    public mutating func addY(_ y:CGFloat){
        let p:CGPoint = CGPoint(x: self.x, y: self.y + y)
        self = p
    }
}
// A sample SwiftUI creating a GameScene and sizing it
// at 300x400 points
struct TTESTContentView: View {
    var scene: SKScene {
        let scene = TTESTGameScene()
        scene.size = CGSize(width: 300, height: 400)
        scene.scaleMode = .aspectFill
        return scene
    }

    var body: some View {
        SpriteView(scene: scene)
            .frame(width: 300, height: 400)
            //.ignoresSafeArea()
    }
}
struct ContentViewTest_Previews: PreviewProvider {
    static var previews: some View {
        TTESTContentView()
    }
}

推荐阅读