Ios 是否有方法将操作添加到所有创建的节点(swift中的一个节点除外)?
我想为场景中除一个或两个节点之外的所有节点添加一个动作 或者,我想找到一种使用“name”方法的不同方式访问所有节点的方法 这不是完整的游戏,但我不想使用“名称”方法,因为每个方块都有不同的名称Ios 是否有方法将操作添加到所有创建的节点(swift中的一个节点除外)?,ios,swift,sprite-kit,enumeration,Ios,Swift,Sprite Kit,Enumeration,我想为场景中除一个或两个节点之外的所有节点添加一个动作 或者,我想找到一种使用“name”方法的不同方式访问所有节点的方法 这不是完整的游戏,但我不想使用“名称”方法,因为每个方块都有不同的名称 import SpriteKit import GameplayKit var Score = 0 class GameScene: SKScene { let SquareSide = 100 var touchedNode = SKNode() var touchLocation = CGPoi
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()
}
导入SpriteKit
导入游戏工具包
风险值得分=0
类游戏场景:SKScene{
设平方边=100
var touchedNode=SKNode()
var touchLocation=CGPoint()
设ScoreLabel=SKLabelNode()
设平方数=0
覆盖func didMove(到视图:SKView){
创建正方形()
}
func{
设square=SKShapeNode(rect:cgrct(x:Int(arc4random_统一(UInt32(自帧宽度))),y:Int(arc4random_统一(UInt32(自帧高度))),宽度:SquareSide,高度:SquareSide))
如果得分,最简单的方法是将要添加操作的所有节点添加到一个数组中,然后在该数组上迭代,在需要时添加操作
如果您的对象庞大而复杂,这会影响内存,但这是无需使用.name
即可实现的最有组织、最快速的方法
而且比在节点层次结构中使用enumerateChildNodes…
etc要快得多,也容易得多,正如我认为您忘记了
事实上,关于这个名字:
要搜索的名称。这可以是
节点或自定义搜索字符串。请参阅
这意味着您还可以执行以下操作:
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
}
}
/
指定搜索应从根节点开始,并在整个节点树上递归执行。否则,它将从其当前位置执行递归搜索
*
表示搜索匹配零个或多个字符。我发现您的问题不清楚,能否请您解释一个但更多,以便我能提供帮助?我想为场景中创建的所有节点添加操作,但一个节点除外(例如LabelNode)但现在在我的代码中,我只有一个节点,我不想为它添加操作,但当我想完成游戏时,有多个节点。不想为它们添加操作。像任何东西一样,使用此功能,但不使用节点的名称:self.EnumerateChildrenodeswithName