我希望随机调用 8 个函数。我知道这些功能单独工作,但是当我尝试使用此代码随机运行它们时,它们不会出现在模拟器中......
func randomizeBuildingFunction() {
let wait = SKAction.wait(forDuration: 6)
let randomBuildings = [createBuildingOne, createBuildingTwo, createBuildingThree, createBuildingFour, createBuildingFive, createBuildingSix, createBuildingSeven, createBuildingEight]
let useRandomResult = SKAction.run {
let randomResult = Int(arc4random_uniform(UInt32(randomBuildings.count)))
return randomBuildings[randomResult]()
}
SKAction.repeatForever(SKAction.sequence([useRandomResult, wait]))
}
randomizeBuildingFunction()
此代码在 .didMove 函数中。这是其中一个功能的示例,因为除了细微的间隔修改和纹理更改之外,它们基本上都是相同的。
func createBuildingOne() {
let one = SKSpriteNode(imageNamed: "BuildingOne.png")
one.anchorPoint = CGPoint(x: 0.5, y: 0.5)
one.physicsBody = SKPhysicsBody(rectangleOf: one.size)
one.physicsBody?.affectedByGravity = false
one.physicsBody?.contactTestBitMask = ColliderType.Buildings.rawValue
one.physicsBody?.categoryBitMask = ColliderType.Buildings.rawValue
one.physicsBody?.collisionBitMask = ColliderType.Buildings.rawValue
self.addChild(one)
one.zPosition = 2
one.position = CGPoint(x: self.frame.width * 0.5, y: -self.frame.height * 0.5 + one.size.height / 1.5)
let moveLeft = SKAction.moveBy(x: -self.frame.width - one.size.width, y: 0, duration: 6)
one.run(SKAction.sequence([moveLeft, SKAction.removeFromParent()]))
}
我在顶部的随机化代码有问题吗?该应用程序构建良好,没有任何错误......
提前致谢!