gpt4 book ai didi

swift - 如何在某个位置随机选择一个节点?

转载 作者:行者123 更新时间:2023-11-28 08:46:46 25 4
gpt4 key购买 nike

我有五个不同颜色的圆圈,每个圆圈都在同一个函数中调用,但具有不同的物理体和名称。我希望它们全部从屏幕底部向上移动,一次只有一个节点在屏幕上从一个设定位置(即 CGPointMake(100, -500))生成随机颜色节点后,在 4几秒后会出现一个不同的随机选择的?

有没有办法让所有的人都被单独识别并做到这一点?

代码:

      func circles() {

let sprites = SKSpriteNode(imageNamed: "Circle1")
sprites.position = CGPointMake(45, -200)

sprites.physicsBody = SKPhysicsBody(circleOfRadius: 40)
sprites.physicsBody?.dynamic = true
sprites.physicsBody?.affectedByGravity = false
sprites.physicsBody?.categoryBitMask = BodyType.player.rawValue
sprites.physicsBody?.contactTestBitMask = BodyType.enemy.rawValue | BodyType.other.rawValue | BodyType.seven.rawValue | BodyType.nine.rawValue | BodyType.five.rawValue

sprites.size = CGSizeMake(45, 45)
all.addChild(sprites)
let sprite2 = SKSpriteNode(imageNamed: "Circle2")
sprite2.position = CGPointMake(60, -750)
sprite2.physicsBody = SKPhysicsBody(circleOfRadius: 40)
sprite2.physicsBody?.categoryBitMask = BodyType.eight.rawValue
sprite2.physicsBody?.contactTestBitMask = BodyType.nine.rawValue | BodyType.other.rawValue | BodyType.five.rawValue | BodyType.enemy.rawValue | BodyType.seven.rawValue
sprite2.physicsBody?.dynamic = true
sprite2.physicsBody?.affectedByGravity = false

sprite2.size = CGSizeMake(45, 45)
addChild(sprite2)
let sprite3 = SKSpriteNode(imageNamed: "circle3")
sprite3.position = CGPointMake(100, -1200)
sprite3.physicsBody = SKPhysicsBody(circleOfRadius: 40)
sprite3.physicsBody?.categoryBitMask = BodyType.six.rawValue
sprite3.physicsBody?.contactTestBitMask = BodyType.seven.rawValue | BodyType.other.rawValue | BodyType.five.rawValue | BodyType.enemy.rawValue | BodyType.nine.rawValue
sprite3.physicsBody?.dynamic = true
sprite3.physicsBody?.affectedByGravity = false

sprite3.size = CGSizeMake(45, 45)
addChild(sprite3)
let sprite4 = SKSpriteNode(imageNamed: "Circle4")
sprite4.position = CGPointMake(150, -1400)
sprite4.physicsBody = SKPhysicsBody(circleOfRadius: 40)
sprite4.physicsBody?.categoryBitMask = BodyType.ground.rawValue
sprite4.physicsBody?.contactTestBitMask = BodyType.other.rawValue | BodyType.player.rawValue | BodyType.five.rawValue | BodyType.enemy.rawValue | BodyType.seven.rawValue
sprite4.physicsBody?.dynamic = true
sprite4.physicsBody?.affectedByGravity = false

sprite4.size = CGSizeMake(45, 45)
addChild(sprite4)
let sprite5 = SKSpriteNode(imageNamed: "Circle5")
sprite5.position = CGPointMake(200, -850)
sprite5.physicsBody = SKPhysicsBody(circleOfRadius: 40)
sprite5.physicsBody?.categoryBitMask = BodyType.ya.rawValue
sprite5.physicsBody?.contactTestBitMask = BodyType.five.rawValue | BodyType.player.rawValue | BodyType.other.rawValue | BodyType.seven.rawValue | BodyType.nine.rawValue


sprite5.physicsBody?.dynamic = true
sprite5.physicsBody?.affectedByGravity = false
sprite5.size = CGSizeMake(45, 45)
addChild(sprite5)
}

非常感谢帮助。

最佳答案

您可以使用 SKNode 的名称属性。

来自documentation :

Used to assign a name to a node for later reference. When choosing a name for a node, you should decide whether each node gets a unique name or whether some nodes will share a common name. If you give the node a unique name, you can find the node later by calling the childNodeWithName: method. If a name is shared by multiple nodes, the name usually means that these are all a similar object type in your game. In this case, you can iterate over those objects by calling the enumerateChildNodesWithName:usingBlock: method.

关于swift - 如何在某个位置随机选择一个节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35121747/

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