除了swift中的一个节点之外,有没有办法对所有创建的节点添加操作?
Is there a way to add action to all the created nodes except one node in swift?
我想为场景中除一两个节点之外的所有节点添加一个动作。
或者,我想找到一种使用"name"方法的不同方式来访问所有节点的方法。
这不是完整的游戏,但我不想使用 "name" 方法,因为每个方块都有不同的名称。
import SpriteKit
import GameplayKit
var Score = 0
class GameScene: SKScene {
let SquareSide = 100
var touchedNode = SKNode()
var touchLocation = CGPoint()
let ScoreLabel = SKLabelNode()
let squareNumber = 0
override func didMove(to view: SKView) {
CreatSquares()
}
func CreatSquares(){
let square = SKShapeNode(rect: CGRect(x: Int(arc4random_uniform(UInt32(self.frame.width))), y: Int(arc4random_uniform(UInt32(self.frame.height))), width: SquareSide, height: SquareSide))
if Score <= 10{
square.fillColor = UIColor.orange}
else{
square.fillColor = UIColor.blue
}
square.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: square.frame.width, height: square.frame.height), center: CGPoint(x: square.position.x, y: square.position.y))
square.physicsBody?.affectedByGravity = false
square.physicsBody?.allowsRotation = false
square.physicsBody?.categoryBitMask = 0
square.name = "square\(number)"
number++
self.addChild(square)
}
}
func CreatScoreLabel(){
let ScoreLabel = SKLabelNode()
ScoreLabel.text = "Your Score is:\(Score)"
ScoreLabel.position = CGPoint(x: self.frame.width/2, y: self.frame.height - 50)
ScoreLabel.color = UIColor.white
self.addChild(ScoreLabel)
}
func updatScore(){
ScoreLabel.text = "Your Score is:\(Score)"
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
touchLocation = touch.location(in: self)
touchedNode = self.atPoint(touchLocation)
if touchedNode.name == "square"{
Score += 1
touchedNode.removeFromParent()
}
}
}
override func update(_ currentTime: TimeInterval) {
updatScore()
}
最简单的方法是将所有要添加动作的节点添加到一个数组中,然后遍历该数组,在 want/need.
时添加动作
如果您的对象又大又复杂,这会占用内存,但这是不使用 .name
.
的最有条理和最快速的方法。
并且比使用 enumerateChildNodes...
等
来遍历节点层次结构要快得多,也容易得多
如 I think you forgot the real power of enumerateChildNodes(withName:
所述
的确,关于名字:
The name to search for. This may be either the literal name of the
node or a customized search string. See Searching the Node Tree.
这意味着您还可以:
self.enumerateChildNodes(withName: "//square*") { node, _ in
// node is equal to each child where the name start with "square"
// in other words, here you can see square0, square1, square2, square3 etc...
if node.name == "square3" {
// do whatever you want with square3
}
}
//
指定搜索应从根节点开始,并在整个节点树中递归执行。否则,它从当前位置执行递归搜索。
*
表示搜索匹配零个或多个字符。
我想为场景中除一两个节点之外的所有节点添加一个动作。
或者,我想找到一种使用"name"方法的不同方式来访问所有节点的方法。
这不是完整的游戏,但我不想使用 "name" 方法,因为每个方块都有不同的名称。
import SpriteKit
import GameplayKit
var Score = 0
class GameScene: SKScene {
let SquareSide = 100
var touchedNode = SKNode()
var touchLocation = CGPoint()
let ScoreLabel = SKLabelNode()
let squareNumber = 0
override func didMove(to view: SKView) {
CreatSquares()
}
func CreatSquares(){
let square = SKShapeNode(rect: CGRect(x: Int(arc4random_uniform(UInt32(self.frame.width))), y: Int(arc4random_uniform(UInt32(self.frame.height))), width: SquareSide, height: SquareSide))
if Score <= 10{
square.fillColor = UIColor.orange}
else{
square.fillColor = UIColor.blue
}
square.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: square.frame.width, height: square.frame.height), center: CGPoint(x: square.position.x, y: square.position.y))
square.physicsBody?.affectedByGravity = false
square.physicsBody?.allowsRotation = false
square.physicsBody?.categoryBitMask = 0
square.name = "square\(number)"
number++
self.addChild(square)
}
}
func CreatScoreLabel(){
let ScoreLabel = SKLabelNode()
ScoreLabel.text = "Your Score is:\(Score)"
ScoreLabel.position = CGPoint(x: self.frame.width/2, y: self.frame.height - 50)
ScoreLabel.color = UIColor.white
self.addChild(ScoreLabel)
}
func updatScore(){
ScoreLabel.text = "Your Score is:\(Score)"
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
touchLocation = touch.location(in: self)
touchedNode = self.atPoint(touchLocation)
if touchedNode.name == "square"{
Score += 1
touchedNode.removeFromParent()
}
}
}
override func update(_ currentTime: TimeInterval) {
updatScore()
}
最简单的方法是将所有要添加动作的节点添加到一个数组中,然后遍历该数组,在 want/need.
时添加动作如果您的对象又大又复杂,这会占用内存,但这是不使用 .name
.
并且比使用 enumerateChildNodes...
等
如
的确,关于名字:
The name to search for. This may be either the literal name of the node or a customized search string. See Searching the Node Tree.
这意味着您还可以:
self.enumerateChildNodes(withName: "//square*") { node, _ in
// node is equal to each child where the name start with "square"
// in other words, here you can see square0, square1, square2, square3 etc...
if node.name == "square3" {
// do whatever you want with square3
}
}
//
指定搜索应从根节点开始,并在整个节点树中递归执行。否则,它从当前位置执行递归搜索。
*
表示搜索匹配零个或多个字符。