0
votes

I am having an issue where my platforms distance becomes messed up after a certain score has been reached. I want the game to start just like the spawnDelayForever part in the touchesBegan until the score reaches 5. After the score reaches 5…10…15 and so on, I want the platforms to be able to speed up with the same amount of space in between each spawn no matter what. The issue is when the score reaches 5, the platforms speed but the distance/space in between each spawn is not the same. They are further apart. Can anybody help me?

Hopefully this didn’t confuse anyone but if it did let me clarify it. I just want the platforms to speed up after a certain score has been reached with the same amount of space between each spawn. Meaning the waitForDuration should speed up (less than 2.0) with each speed increase.

Part of my code:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    if gameStarted == false {

        gameStarted = true

        ball.physicsBody?.affectedByGravity = true

        title.removeFromParent()

        let spawn = SKAction.runBlock({

            ()in

            self.creatingPlatforms()


        })

        let delay = SKAction.waitForDuration(2.0)
        let spawnDelay = SKAction.sequence([spawn, delay])
        let spawnDelayForever = SKAction.repeatActionForever(spawnDelay)
        self.runAction(spawnDelayForever)

        let distance = CGFloat(self.frame.width + 170 + officialPlatform.frame.width)
        let movePlatforms = SKAction.moveByX(-distance, y: 0, duration: NSTimeInterval(0.01 * distance))
        let removePlatforms = SKAction.removeFromParent()
        moveAndRemove = SKAction.sequence([movePlatforms, removePlatforms])


        ball.physicsBody?.velocity = CGVectorMake(0, 0)
        ball.physicsBody?.applyImpulse(CGVectorMake(0, 0))

        scoreLbl.fontSize = 35
        scoreLbl.position = CGPoint(x: self.frame.width / 2, y: scene!.frame.height / 2 + 300)
        scoreLbl.fontColor = UIColor.blackColor()
        scoreLbl.text = "\(score)"
        score = 0
        self.addChild(scoreLbl)



    }

    else {

        ball.physicsBody?.velocity = CGVectorMake(0, 0)
        ball.physicsBody?.applyImpulse(CGVectorMake(0, 45))

        ball.physicsBody?.restitution = 0
        scoreNode.physicsBody?.restitution = 0

    }
}





func creatingPlatforms() {

    scoreNode = SKSpriteNode()

    scoreNode.size = CGSize(width: 120, height: 25)
    scoreNode.position = CGPoint(x: self.frame.width + 95, y: self.frame.height / 2)
    scoreNode.physicsBody = SKPhysicsBody(rectangleOfSize: scoreNode.size)
    scoreNode.physicsBody?.affectedByGravity = false
    scoreNode.physicsBody?.dynamic = false
    scoreNode.physicsBody?.categoryBitMask = physicsCategory.score
    scoreNode.physicsBody?.collisionBitMask = 0
    scoreNode.physicsBody?.contactTestBitMask = physicsCategory.ball
    scoreNode.name = "scoreNode"

    officialPlatform = SKNode()

    let platform = SKSpriteNode(imageNamed: "platform")

    platform.size = CGSize(width: 140, height: 25)
    platform.position = CGPoint(x: self.frame.width + 95, y: self.frame.height / 2)
    platform.physicsBody = SKPhysicsBody(rectangleOfSize: platform.size)
    platform.physicsBody?.categoryBitMask = physicsCategory.platform
    platform.physicsBody?.collisionBitMask = physicsCategory.ball
    platform.physicsBody?.contactTestBitMask = physicsCategory.ball
    platform.physicsBody?.affectedByGravity = false
    platform.physicsBody?.dynamic = false

    officialPlatform.name = "official platform"

    officialPlatform.runAction(moveAndRemove)

    officialPlatform.addChild(platform)
    officialPlatform.addChild(scoreNode)

    self.addChild(officialPlatform)




}

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

    if score >= 5 && score <= 9 {

        moveAndRemove.speed = 0.9



      }

   }
}
1
Could you add them all as a child to one larger node, and just move that larger node?Beau Nouvelle
@BeauNouvelle I'm not sure if you mean the platform or something else, but the platform that is spawning is in a larger node (let platform = SKSpriteNode) = (officialPlatform = SKNode). The SKNode, officialPlatform, is running the action moveAndRemove which is called in touchesBegan. The issue is how can the SKActions spawnDelayForever and moveAndRemove be updated each time a certain score has been reached?behind the aura

1 Answers

0
votes

I found my answer in another question on here. swift - Increase speed of objects over time

I changed my self.runAction(spawnDelayForever) in my touchesBegan to self.runAction(spawnDelayForever, withKey: "spawnDelayForever"). Then in the update function, I changed it to:

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

    if score > 0  {

        self.actionForKey("spawnDelayForever")?.speed += 0.001
        moveAndRemove.speed += 0.001

    }

}