0

我希望随机调用 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()]))
        }

我在顶部的随机化代码有问题吗?该应用程序构建良好,没有任何错误......

提前致谢!

4

1 回答 1

0

感谢@Knight0fDragon 的评论!是的,你是对的,经过几个小时想知道我错过了什么,以及我的一个苹果开发人员朋友的一点帮助,我终于意识到我必须以某种方式调用运行块......

因此,我的问题的答案是这两行代码:

let useFunctionForever = SKAction.repeatForever(SKAction.sequence([useRandomResult, wait]))

    run(useFunctionForever)

我必须运行 repeatForever 操作,然后它会调用并运行我的块。我已经有了repeatForever Action,但我的错误是期望它在调用整个函数时自动运行。所以我想我的问题不一定与 arc4Random 相关,而是与我的代码的 repeatForever 部分有关。

于 2018-07-09T14:36:48.600 回答