Search code examples
iosswiftsprite-kitdelay

swift - Increase speed of objects over time


I'm looking for a way to increase the pace of my game the longer you play it, I would like to achieve this by increasing the frequency of obstacles generated either after a certain time i.e. every 30 seconds or preferably after 10 objects (trees), have been generated so the longer you play the harder it gets.

This is my current set up, I use repeatActionForever how could I change this to something like repeatAction10Times with a different delay variable for each loop?

//in didMoveToView

    treeTexture1 = SKTexture(imageNamed: "tree")
    treeTexture1.filteringMode = SKTextureFilteringMode.Nearest

    var distanceToMove = CGFloat(self.frame.size.width + 0.1);
    var moveTrees = SKAction.moveByX(-distanceToMove, y:0, duration:NSTimeInterval(0.006 * distanceToMove));
    var removeTrees = SKAction.removeFromParent();
    moveAndRemoveTrees = SKAction.sequence([moveTrees, removeTrees]);

    var spawn = SKAction.runBlock({() in self.spawnTrees()})

//this delay is what I would like to alter for each loop

    var delay = SKAction.waitForDuration(NSTimeInterval(1.2))

    var spawnThenDelay = SKAction.sequence([spawn, delay])
    var spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)
    self.runAction(spawnThenDelayForever)


func spawnTrees() {
    var tree = SKNode()
    tree.position = CGPointMake( self.frame.size.width + treeTexture1.size().width * 2, 0 );
    tree.zPosition = -10;

    var height = UInt32( self.frame.size.height / 1 )
    var height_max = UInt32( 220 )
    var height_min = UInt32( 100 )
    var y = arc4random_uniform(height_max - height_min + 1) + height_min;

    var tree1 = SKSpriteNode(texture: treeTexture1)
    tree1.position = CGPointMake(0.0, CGFloat(y))
    tree1.physicsBody = SKPhysicsBody(rectangleOfSize: tree1.size)
    tree1.physicsBody?.dynamic = false
    tree1.physicsBody?.categoryBitMask = treeCategory;
    tree1.physicsBody?.collisionBitMask = 0
    tree1.physicsBody?.contactTestBitMask = 0
    tree.addChild(tree1)



    tree.runAction(moveAndRemoveTrees)

    trees.addChild(tree)

}

Solution

  • You should try to use the simple action.speed code.

    For example: Instead of running the action spawnThenDelay ten times and then running some code and repeating, try making a counter. Create a global variable at the very top of the code called counter, or whatever you want to call it. In spawnTrees(), change the code to this:

    func spawnTrees() {
        var tree = SKNode()
        tree.position = CGPointMake( self.frame.size.width + treeTexture1.size().width * 2, 0 );
        tree.zPosition = -10;
        counter++
    
        ... }
    

    And then in the update(), check to see if counter is above 10.

    if counter == 10 {
         self.actionForKey("spawnThenDelayForever").speed += 10.0 // Or some integer/float like that
         counter = 0
    }
    

    Now, what this will do is run the code inside that if-statement for every 10 times you spawn something. But to do this, you'll have to update your calling of spawnAndThenDelayForever to add a key to reference it with.

    self.runAction(spawnThenDelayForever, withKey "spawnThenDelayForever")
    

    Let me know if there are any syntactical errors in what I gave you, or if it doesn't work quite right.