Spritekit上的双重生成(和重叠)敌人

问题描述 投票:1回答:1

我在无休止的运行游戏中遇到了麻烦,主要是因为在我创建的两种敌人身上生成了东西,有时当我们调用函数“ startDifficultyTimer”时会出现两个敌人生成的情况。基本上,当我调用该函数时,游戏会在同一行中生成2个节点,因此玩家被迫输了,我想避免这种情况,但我不知道如何,我尝试了几乎所有操作,并尝试从didmove中删除了createequentialenemy但是问题仍然存在,我认为(作为新手)问题出在startDifficultTimer函数中,因为当值达到极限时,问题就消失了我已经在下面发布了代码,对于可能犯下的巨大错误感到抱歉,但是我们是快速开发游戏的新手,非常感谢大家!

func createEnemy() {
    let enemy: Enemy
    let duration: CGFloat

    switch Int(arc4random() % 100) {
        case 0...70:
            enemy = Enemy.createEnemy()
            duration =  CGFloat(Float(arc4random()%1)) + durationV
            enemy.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 55, height: 37))
            let enemyf = enemy.frame
            let fixedx = frame.width + enemy.frame.width/2.0
            let positions = [ CGPoint(x: fixedx, y: 383), CGPoint(x: fixedx, y: 447), CGPoint(x: fixedx, y: 511)]
            let position = positions[Int(arc4random_uniform(UInt32(positions.count)))]
            enemy.position = position
        case 71...100:
            enemy = Enemy.createEnemyMedium()
            duration =  CGFloat(Float(arc4random()%1)) + durationV
            enemy.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 40, height: 70))
            let enemyf = enemy.frame
            let fixedx = frame.width + enemy.frame.width/2.0
            let positions = [ CGPoint(x: fixedx, y: 415), CGPoint(x: fixedx, y: 479)]
            let position = positions[Int(arc4random_uniform(UInt32(positions.count)))]
            enemy.position = position
        default:
            enemy = Enemy.createEnemy()
            //type = .small
            duration =  CGFloat(Float(arc4random()%1)) + durationV
            let enemyf = enemy.frame
            let fixedx = frame.width + enemy.frame.width/2.0
            let positions = [ CGPoint(x: fixedx, y: 383), CGPoint(x: fixedx, y: 447), CGPoint(x: fixedx, y: 511)]
            let position = positions[Int(arc4random_uniform(UInt32(positions.count)))]
            let texture = SKTexture(imageNamed: "dronea1")
            enemy.position = position
    }

    enemy.physicsBody!.isDynamic = false
    enemy.physicsBody!.categoryBitMask = PhysicsCategory.Enemy
    addChild(enemy)

    let moveTo = SKAction.moveTo(x: 0.0, duration: TimeInterval(duration))
    enemy.run(.repeatForever(.sequence([moveTo, .removeFromParent()])))
}

func createSequentialEnemies() {
    // remove previous action if running. This way you can adjust the spawn duration property and call this method again and it will cancel previous action.
    removeAction(forKey: spawnKey)
    let spawnAction = SKAction.run(createEnemy)
    let spawnDelay = SKAction.wait(forDuration: spawnDuration)
    let spawnSequence = SKAction.sequence([spawnAction, spawnDelay])
    run(SKAction.repeatForever(spawnSequence), withKey: spawnKey)later
}

func startDifficultyTimer() {
    let difficultyTimerKey = "DifficultyTimerKey"
    let action1 = SKAction.wait(forDuration: 1)
    let action2 = SKAction.run { [unowned self] in
        guard self.spawnDuration > 0.5 else {  // set a min limit
            self.removeAction(forKey: difficultyTimerKey) // if min duration has been reached than you might as well stop running this timer.
            return
        }
        self.spawnDuration -= 0.5 // reduce by half a second
        self.createSequentialEnemies() // spawn enemies again
    }
    let sequence = SKAction.sequence([action1, action2])
    run(SKAction.repeatForever(sequence), withKey: difficultyTimerKey)
}
ios swift xcode sprite-kit
1个回答
1
投票
© www.soinside.com 2019 - 2024. All rights reserved.