swift - 如何将一组skspritenodes一起移动
问题描述
我想将一组 skspritenodes 一起移动。我已经把它们放在同一个名字下。但我想将它们作为一个整体向左移动。我怎样才能做到这一点?每个节点都有不同的 x 和 y 位置,那么我可以做些什么来改变节点的位置,使它们都保持在一起但横向移动?
解决方案
您可以通过搜索将 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()
}
}
推荐阅读
- python - 多类图像分类中如何获取权重图
- javascript - 为什么在修改参数时添加默认参数值会改变行为?
- python - 从 numpy 数组以 HTML 格式渲染音频
- php - 如何在 varchar 列中使用 Laravel whereYear?
- c# - 基于按钮单击更改组合框值 c# wpf
- android - 应用程序运行时如何停止通知?
- azure - 如何使用 powershell 列出订阅中所有虚拟机的所有入站规则?
- typescript - 更改打字稿目标会影响编译性能吗?
- android - 在 Microsoft Azure VDI 中为 iOS 开发设置 Mac OS 环境
- flutter - 是否有类似于 CombineLatest 仅适用于 1 个流的功能?