Spritekit - 提高速度并保持精灵节点之间的距离相同?

时间:2016-01-22 21:48:08

标签: swift sprite-kit

我遇到的问题是,在达到某个分数后,我的平台距离会变得混乱。我希望游戏的开始就像touchesBegan中的spawnDelayForever部分一样,直到得分达到5.在得分达到5 ... 10 ... 15之后,依此类推,我希望平台能够以相同的空间加速每个产卵之间无论如何。问题是当分数达到5时,平台加速但每个产卵之间的距离/空间不一样。它们相距甚远。有谁能够帮我?

希望这并不会让任何人感到困惑,但如果确实让我澄清了。我只是希望平台在达到某个分数后加速,每个产生之间的空间相同。这意味着随着每次速度的提高,waitForDuration应该加速(小于2.0)。

我的部分代码:

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 个答案:

答案 0 :(得分:0)

我在这里找到了另一个问题的答案。 swift - Increase speed of objects over time

我在我的触摸中将public string ZodziuSkaiciavimas() { foreach (var sentence in Sakiniai.TrimEnd('.').Split('.')) { Console.WriteLine(eilute.ToString() + " sakinyje zodziu:" + (sentence.Trim().Split(' ').Count() + sentence.Trim().Split('-').Count() + sentence.Trim().Split(';').Count() + sentence.Trim().Split(':').Count() + sentence.Trim().Split(',').Count() - 4)); eilute++; } return Rezultatas; } 改为self.runAction(spawnDelayForever)。然后在更新功能中,我将其更改为:

self.runAction(spawnDelayForever, withKey: "spawnDelayForever")