简体   繁体   中英

Spritekit - Increasing speed and keeping the same distance between sprite nodes?

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



      }

   }
}

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

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM