0

我第一次调用它时效果很好。敌人被加载到我想要的位置……目标是当当前一波敌人被击败时……第二波敌人以完全相同的重复方式加载。

我第二次调用“loadPatternEnemies(number:Int)”时得到“in here?” 在控制台中......但没有添加敌人......

我在这里做错了什么?

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)
        }
    }
}
4

0 回答 0