【问题标题】:how to move a group of skspritenodes together如何将一组skspritenodes一起移动
【发布时间】:2021-12-05 21:45:59
【问题描述】:

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

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: swift sprite-kit


【解决方案1】:

要么将第 2 个和后续节点设为第一个节点的子节点,要么将它们全部设为某个可能的不可见节点的子节点。

然后移动父级也会移动所有子级。您可能需要根据 X 和 Y 位置调整创建节点的方式,因为它们是相对于父节点的。

【讨论】:

    【解决方案2】:

    您可以通过搜索将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()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多