如果游戏持续的时间越长,我怎么能让我的炸弹下降更快或产生更快?

时间:2016-02-08 06:41:44

标签: ios swift sprite-kit nstimer skaction

如果游戏持续的时间越长,我怎样才能使我的炸弹下降得更快或产生更快?我想让你的比赛越久,你就越努力。我希望每10枚炸弹变得更难。我已经研究了几天,并尝试了类似问题的多个答案。我相信我制造炸弹的方式是让我更难做到这一点。提前谢谢!

var bombSpawnSpeed : NSTimeInterval = 0.40
var bombCounter : Int = 0

这是我的更新功能。我的理解是这只是整个场景的常规更新功能。

  override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

  if bombCounter >= 10 {
        if bombSpawnSpeed > 0.15 {
            bombSpawnSpeed -= 0.05
            bombCounter = 0

        }
        else {
            bombSpawnSpeed = 0.1

        }

    }

这是炸弹

func enemies() {

    let bomb1 = SKSpriteNode(imageNamed: "AtomicBomb")
    let bomb2 = SKSpriteNode(imageNamed: "EMP")
    let bomb3 = SKSpriteNode(imageNamed: "BioBomb")
    let bomb4 = SKSpriteNode(imageNamed: "ChemBomb")

    let enemy = [bomb1, bomb2, bomb3, bomb4]


    // Enemy Physics
    for bomb in enemy {
        bomb1.size = CGSize(width: 55, height: 55)
        bomb2.size = CGSize(width: 55, height: 70)
        bomb3.size = CGSize(width: 30, height: 30)
        bomb4.size = CGSize(width: 45, height: 70)


        bomb.physicsBody = SKPhysicsBody(circleOfRadius: bomb.size.width / 4)
        bomb.physicsBody?.categoryBitMask = PhysicsCategory.enemy
        bomb.physicsBody?.collisionBitMask = PhysicsCategory.missile | PhysicsCategory.airDefense
        bomb.physicsBody?.contactTestBitMask = PhysicsCategory.missile | PhysicsCategory.airDefense
        bomb.physicsBody?.affectedByGravity = false 
        bomb.runAction(SKAction.speedBy(10, duration: 30))
        bomb.physicsBody?.dynamic = true
        bomb1.name = "enemy1"
        bomb2.name = "enemy2"
        bomb3.name = "enemy3"
        bomb4.name = "enemy4"

        print("bomb")

    }

    let deployAtRandomPosition = arc4random() % 4

    switch deployAtRandomPosition {
    case 0:
        bomb1.position.y = frame.size.height

        let PositionX = arc4random_uniform(UInt32(frame.size.width))

        bomb1.position.x = CGFloat(PositionX)

        self.addChild(bomb1)

        break

    case 1:
        bomb2.position.y = frame.size.height

        let PositionX = arc4random_uniform(UInt32(frame.size.width))

        bomb2.position.x = CGFloat(PositionX)

        self.addChild(bomb2)

        break

    case 2:
        bomb3.position.y = frame.size.height

        let PositionX = arc4random_uniform(UInt32(frame.size.width))

        bomb3.position.x = CGFloat(PositionX)

        self.addChild(bomb3)

        break

    case 3:
        bomb4.position.y = frame.size.height

        let PositionX = arc4random_uniform(UInt32(frame.size.width))

        bomb4.position.x = CGFloat(PositionX)

        self.addChild(bomb4)

        break

    default:

        break

    }

   bomb1.runAction(SKAction.moveTo(airDefense.position, duration: 5))
   bomb2.runAction(SKAction.moveTo(airDefense.position, duration: 5))
   bomb3.runAction(SKAction.moveTo(airDefense.position, duration: 5))
   bomb4.runAction(SKAction.moveTo(airDefense.position, duration: 5))

}

这就是我所要求的接触开始制造炸弹

enemyTimer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("enemies"), userInfo: nil, repeats: true)

2 个答案:

答案 0 :(得分:3)

我认为更好的方法是

  1. 添加名为lastTimeAddedBomb

  2. 的新变种
  3. 通过跟踪您释放的炸弹数来确定您的间隔时间

  4. 检查是否需要在update方法
  5. 中添加新炸弹

    像这样:

    var lastTimeAddedBomb:NSDate
    var numberOfBombsDropped:Int = 0
    var difficultyLevel:Float = 1
    
    override func update(currentTime: CFTimeInterval) {
    
        if (numberOfBombsDropped % 10 == 0) {
            difficultyLevel-= 0.1;
        }
    
        let intervalForBombs = difficultyLevel
    
        let timeFromLastBomb = NSDate().timeIntervalSinceDate(lastTimeAddedBomb)
        if (timeFromLastBomb >= intervalForBombs) {
            //call bomb
            //save last bomb date as now
            numberOfBombsDropped++
            lastTimeAddedBomb = NSDate()
        } 
    }
    

答案 1 :(得分:3)

你的代码没有增加bombCounter,它确实将bombSpawnSpeed重置为0.1中的10个更新。 更新方法可能是经常减少bombSpawnSpeed。但如果速度很好,你可以使用它。 如果你不在别处增加bombCounter,你可以在更新方法中增加它。 知道你最后一次投下炸弹的变量也会很好。

var lastDroppedBombTime: NSDate

override func update(currentTime: CFTimeInterval) {
    bombCounter += 1
    if bombCounter >= 10 {
        bombSpawnSpeed -= 0.05
        bombCounter = 0
    }

    let differenceInTime = NSDate().timeIntervalSinceDate(lastDroppedBombTime) // or use the difference of the parameter currentTime and lastDroppedBombTime
    if (differenceInTime >= bombSpawnSpeed) {
        enemies() // spawn the new bomb(s)
        lastDroppedBombTime = NSDate()
    }
}