gpt4 book ai didi

ios - TouchBegan/TouchEnded 数组

转载 作者:可可西里 更新时间:2023-11-01 02:20:05 27 4
gpt4 key购买 nike

我有 3 个节点数组,每个数组有 5 个节点。本例中的节点是正方形。

我想使用 touchesBegan 和 touchesEnded 移动它们,保存用户触摸的数组,然后保存手指从屏幕上移开时的位置。我已经知道如何使用节点来做到这一点。

我的问题是我不知道如何告诉我的代码要移动哪个数组,因为我不能使用 array.name 之类的东西来区分差异我怎么能做这样的事情?

例如,如果我触摸我的 Array1,他会检测到它是我的 Array1,然后当我移开手指时,他会执行 SKAction 以移动我的 Array1 内的节点。

我尝试使用 array.description 但没有用。

谢谢。

最佳答案

由于 Sprite Kit 提供了访问场景节点树中 sprite 的便捷方法,因此几乎没有理由使用数组来管理您的 sprite 节点。在这种情况下,您可以为 Sprite 添加一组 SKNode,因为您可以使用 node = sprite.parent 轻松访问 Sprite 所在的“容器”。然后,您可以通过遍历 node.children 来迭代该容器中的 Sprite 。以下是如何执行此操作的示例:

var selectedNode:SKSpriteNode?

override func didMoveToView(view: SKView) {
scaleMode = .ResizeFill

let width = view.frame.size.width
let height = view.frame.size.height

let colors = [SKColor.redColor(), SKColor.greenColor(), SKColor.blueColor()]

// Create 3 container nodes
for i in 1...3 {
let node = SKNode()
// Create 5 sprites
for j in 1...5 {
let sprite = SKSpriteNode(imageNamed:"Spaceship")
sprite.color = colors[i-1]
sprite.colorBlendFactor = 0.5
sprite.xScale = 0.125
sprite.yScale = 0.125
// Random location
let x = CGFloat(arc4random_uniform(UInt32(width)))
let y = CGFloat(arc4random_uniform(UInt32(height)))
sprite.position = CGPointMake(x, y)
// Add the sprite to a container
node.addChild(sprite)
}
// Add the container to the scene
addChild(node)
}
}

选择要在 touchesBegan 中移动的 Sprite

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)
selectedNode = node as? SKSpriteNode
}
}

移动选中的 Sprite

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
selectedNode?.position = location
}
}

旋转包含所选 Sprite 的节点中的所有 child

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
if let parent = selectedNode?.parent?.children {
for child in parent {
let action = SKAction.rotateByAngle(CGFloat(M_PI*2.0), duration: 2.0)
child.runAction(action)
}
}
selectedNode = nil
}

关于ios - TouchBegan/TouchEnded 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31328887/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com