SWIFT:如何随机选择5个障碍中的1个并对其进行操作,并每秒重复此过程?

时间:2016-07-09 20:42:48

标签: swift random sprite-kit

我正在尝试创建一个游戏,其中有五个不同的障碍物,其中一个每秒随机选择,并从屏幕顶部移动到屏幕底部。这应该为玩家创建一个障碍场进行导航。我能够让第一个障碍物在屏幕上向下移动,但是在一秒钟后屏幕上出现了另一个障碍,我不断尝试修复问题,不断获得线程1:信号SIGABRT错误。

这是我的代码:

func randomize() {
    smallMiddleObstacle.size = CGSizeMake(self.frame.width - 180, obstacleHeight)
    smallMiddleObstacle.position = CGPointMake(self.frame.width / 2, self.frame.height + smallMiddleObstacle.frame.height / 4)
    smallMiddleObstacle.color = UIColor.blueColor()

    bigMiddleObstacle.size = CGSizeMake(self.frame.width - 100, obstacleHeight)
    bigMiddleObstacle.position = CGPointMake(self.frame.width / 2, self.frame.height + bigMiddleObstacle.frame.height / 4)
    bigMiddleObstacle.color = UIColor.blueColor()

    rightObstacle.size = CGSizeMake(self.frame.width * 1.4, obstacleHeight)
    rightObstacle.position = CGPointMake(self.frame.width, self.frame.height + rightObstacle.frame.height / 4)
    rightObstacle.color = UIColor.blueColor()

    leftObstacle.size = CGSizeMake(self.frame.width * 1.4, obstacleHeight)
    leftObstacle.position = CGPointMake(0, self.frame.height + leftObstacle.frame.height / 4)
    leftObstacle.color = UIColor.blueColor()

    rightObstacleInPair.size = CGSizeMake(self.frame.width * 0.7, obstacleHeight)
    rightObstacleInPair.position.x = self.frame.width
    rightObstacleInPair.color = UIColor.blueColor()

    obstaclePair.addChild(rightObstacleInPair)

    leftObstacleInPair.size = CGSizeMake(self.frame.width * 0.7, obstacleHeight)
    leftObstacleInPair.position.x = 0
    leftObstacleInPair.color = UIColor.blueColor()

    obstaclePair.addChild(leftObstacleInPair)

    obstaclePair.position.y = self.frame.height + obstaclePair.frame.height / 4

    let distance = CGFloat(self.frame.height + obstacleHeight)
    let move = SKAction.moveByX(0, y: -distance, duration: NSTimeInterval(0.005 * distance))
    let remove = SKAction.removeFromParent()
    moveAndRemove = SKAction.sequence([move, remove])

    let random = Int(arc4random_uniform(4))

    if random == 0 {
        addChild(smallMiddleObstacle)
        smallMiddleObstacle.runAction(moveAndRemove)
    } else if random == 1 {
        addChild(bigMiddleObstacle)
        bigMiddleObstacle.runAction(moveAndRemove)
    } else if random == 2 {
        addChild(rightObstacle)
        rightObstacle.runAction(moveAndRemove)
    } else if random == 3 {
        addChild(leftObstacle)
        leftObstacle.runAction(moveAndRemove)
    } else {
        addChild(obstaclePair)
        obstaclePair.runAction(moveAndRemove)
    }

}



override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if !isGameStarted {

        isGameStarted = true


        let spawn = SKAction.runBlock({
        () in

            self.randomize()

    })

        let delay = SKAction.waitForDuration(1.5)
        let spawnDelay = SKAction.sequence([spawn, delay])
        let spawnDelayForever = SKAction.repeatActionForever(spawnDelay)

        self.runAction(spawnDelayForever)



    }



}

由于

1 个答案:

答案 0 :(得分:1)

您忘记了removeFromParent已经addChild,所以,第二次调用randomize方法时,您有&#34;尝试添加已经存在的SKNode有父母:&#34;

首先,如果您尚未完成,请为您的节点提供name

每次 之前你制作addChild,你必须确定你还没有添加它,知道你可以这样做:

例如:

if let child = self.childNodeWithName(smallMiddleObstacle.name) {
     child.removeFromParent()
}