定时器未触发

时间:2018-03-20 00:41:48

标签: swift arkit scnscene

我第一次打电话给它,效果很好。敌人被装载在我喜欢的地方......目标是当前的敌人波被击败......第二波敌人以完全相同的重复方式加载。

我第二次调用" loadPatternEnemies(数字:Int)"我在这里得到了#34;"在控制台......但没有添加敌人...

我在这里做错了什么?

func loadPatternEnemies(number:Int) {
        switch number {
        case 0:
            print("in here?")
            let parentTimer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(self.addEnemyToScene(timer:)), userInfo: nil, repeats: true)
            enemySpawnTimers.append(parentTimer)
            enemiesInCurrentWaveLeftToKill = positions.count
        default:
            print("default reached")
        }
    }

@objc func addEnemyToScene(timer: Timer) {
    if indexForPositions == positions.count {
        enemySpawnTimers[currentWaveNumber].invalidate()
        indexForPositions = 0
    } else {
        var newEnemy = Enemy()
        newEnemy.health = 1
        newEnemy.position = SCNVector3(positions[indexForPositions].x,positions[indexForPositions].y,positions[indexForPositions].z)
        newEnemy.name = "enemy\(enemiesInSceneArray.count)"
        newEnemy.timerToShoot = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.enemyFireBullet(timer:)), userInfo: newEnemy, repeats: true)
        enemiesInSceneArray.append(newEnemy)

        let enemyNode = SCNNode(geometry: SCNSphere(radius: 2))
        enemyNode.position = newEnemy.position
        enemyNode.name = newEnemy.name
        enemyNode.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(geometry: enemyNode.geometry!, options: nil))
        enemyNode.physicsBody?.categoryBitMask = BitMaskCategory.enemy.rawValue
        self.sceneView.scene.rootNode.addChildNode(enemyNode)

        indexForPositions += 1
    }
}

func enemyHit(bulletNode : SCNNode, enemyNode : SCNNode) {
    bulletNode.removeFromParentNode()
    if let index = enemiesInSceneArray.index(where: { $0.name == enemyNode.name }) {
        enemiesInSceneArray[index].health -= 1
        if enemiesInSceneArray[index].health <= 0 {
            enemiesInSceneArray[index].timerToShoot.invalidate()
            enemyNode.removeFromParentNode()
            score += 1
            enemiesInCurrentWaveLeftToKill -= 1
        }

        if enemiesInCurrentWaveLeftToKill == 0 && score > 2 {
            currentWaveNumber += 1
            loadPatternEnemies(number: 0)
        }
    }
}

0 个答案:

没有答案