计算随机平台时,SKAction“延迟”期间的帧率下降

时间:2018-12-16 04:33:05

标签: sprite-kit delay frame-rate spawn skaction

我正在制作一款在玩家面前生成随机平台的游戏。在touchs begin方法中,我创建了一个SKAction,它在延迟后调用了create wall函数。使用Xcode工具,我缩小了范围,每次生成平台时的断续性/帧率下降都植根于createWalls(),并相信它是试图从随机数中绘制精灵。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    if isGameStarted == false{
        //1
        isGameStarted =  true
        player.physicsBody?.affectedByGravity = true
        createPauseBtn()
        //2
        logoImg.run(SKAction.scale(to: 0.5, duration: 0.3), completion: {
            self.logoImg.removeFromParent()
        })
        taptoplayLbl.removeFromParent()
        //3
        self.player.run(repeatActionBird)

        //TODO: add pillars here

        let spawn = SKAction.run({
            () in
            self.wallPair = self.createWalls()
            self.addChild(self.wallPair)
        })
        //2
        let delay = SKAction.wait(forDuration: 1.5
        )
        let SpawnDelay = SKAction.sequence([spawn, delay])
        let spawnDelayForever = SKAction.repeatForever(SpawnDelay)
        self.run(spawnDelayForever)
        //3
        let distance = CGFloat(self.frame.width + wallPair.frame.width)
        let movePillars = SKAction.moveBy(x: -distance - 50, y: 0, duration: TimeInterval(0.008 * distance))
        let removePillars = SKAction.removeFromParent()
        moveAndRemove = SKAction.sequence([movePillars, removePillars])

        player.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
        player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 40))
    } else {
        //4
        if isDied == false {
            player.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
            player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 40))
        }
    }


    for touch in touches{
        let location = touch.location(in: self)
        //1
        if isDied == true{
            if restartBtn.contains(location){
                if UserDefaults.standard.object(forKey: "highestScore") != nil {
                    let hscore = UserDefaults.standard.integer(forKey: "highestScore")
                    if hscore < Int(scoreLbl.text!)!{
                        UserDefaults.standard.set(scoreLbl.text, forKey: "highestScore")
                    }
                } else {
                    UserDefaults.standard.set(0, forKey: "highestScore")
                }
                restartScene()
            }
        } else {
            //2
            if pauseBtn.contains(location){
                if self.isPaused == false{
                    self.isPaused = true
                    pauseBtn.texture = SKTexture(imageNamed: "play")
                } else {
                    self.isPaused = false
                    pauseBtn.texture = SKTexture(imageNamed: "pause")
                }
            }
        }
    }
}

这是我的createWalls()函数

 func createWalls() -> SKNode  {
    // 1
    let coinPouch = SKSpriteNode()
    coinPouch.size = CGSize(width: 40, height: 40)
    coinPouch.position = CGPoint(x: self.frame.width + 25, y: self.frame.height / 2)
    coinPouch.physicsBody = SKPhysicsBody(rectangleOf: coinPouch.size)
    coinPouch.physicsBody?.affectedByGravity = false
    coinPouch.physicsBody?.isDynamic = false
    coinPouch.physicsBody?.categoryBitMask = CollisionBitMask.flowerCategory
    coinPouch.physicsBody?.collisionBitMask = 0
    coinPouch.physicsBody?.contactTestBitMask = CollisionBitMask.birdCategory
    coinPouch.color = SKColor.blue
    // 2
    wallPair = SKNode()
    wallPair.name = "wallPair"

    let randomPlatform1 = Int(arc4random_uniform(4)) + 1
    let randomPlatform2 = Int(arc4random_uniform(4)) + 1

    let platformAtlas = SKTextureAtlas(named: "CoinPlatforms")
    let texture1 = platformAtlas.textureNamed("\(randomPlatform1)CoinPlatform")
    let texture2 = platformAtlas.textureNamed("\(randomPlatform2)CoinPlatform")

    let topWall = SKSpriteNode(texture: texture1)
    topWall.name = "\(randomPlatform1)CoinPlatform"

    let btmWall = SKSpriteNode(texture: texture2)
    btmWall.name = "\(randomPlatform2)CoinPlatform"

    topWall.position = CGPoint(x: self.frame.width + 25, y: self.frame.height / 2 + 475)
    btmWall.position = CGPoint(x: self.frame.width + 25, y: self.frame.height / 2 - 475)

    topWall.setScale(0.5)
    btmWall.setScale(0.5)

    if randomPlatform1 < 4 {
    topWall.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "\(randomPlatform1)CoinPlatform"), size: topWall.size)
        topWall.physicsBody?.categoryBitMask = CollisionBitMask.pillarCategory
        topWall.physicsBody?.collisionBitMask = CollisionBitMask.birdCategory
        topWall.physicsBody?.contactTestBitMask = CollisionBitMask.birdCategory
    } else {
        topWall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: topWall.size.width - 2, height: topWall.size.height - 2))
        topWall.physicsBody?.categoryBitMask = CollisionBitMask.groundCategory
        topWall.physicsBody?.collisionBitMask = CollisionBitMask.birdCategory
        topWall.physicsBody?.contactTestBitMask = CollisionBitMask.birdCategory
    }




    topWall.physicsBody?.isDynamic = false
    topWall.physicsBody?.affectedByGravity = false

    if randomPlatform2 < 4 {
        btmWall.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "\(randomPlatform2)CoinPlatform"), size: btmWall.size)
        btmWall.physicsBody?.categoryBitMask = CollisionBitMask.pillarCategory
        btmWall.physicsBody?.collisionBitMask = CollisionBitMask.birdCategory
        btmWall.physicsBody?.contactTestBitMask = CollisionBitMask.birdCategory
    } else {
        btmWall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: btmWall.size.width - 2, height: btmWall.size.height - 2))
        btmWall.physicsBody?.categoryBitMask = CollisionBitMask.groundCategory
        btmWall.physicsBody?.collisionBitMask = CollisionBitMask.birdCategory
        btmWall.physicsBody?.contactTestBitMask = CollisionBitMask.birdCategory
    }


    btmWall.physicsBody?.isDynamic = false
    btmWall.physicsBody?.affectedByGravity = false

    topWall.zRotation = CGFloat(Double.pi)

    wallPair.addChild(topWall)
    wallPair.addChild(btmWall)

    wallPair.zPosition = 1

    let randomPosition = random(min: -200, max: 200)
    wallPair.position.y = wallPair.position.y +  randomPosition
    let randomSuperCoinSpawn = arc4random_uniform(5)
    if randomSuperCoinSpawn == 1 {
    wallPair.addChild(coinPouch)
    }

    wallPair.run(moveAndRemove)

    return wallPair
}

func random() -> CGFloat{
    return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}

func random(min : CGFloat, max : CGFloat) -> CGFloat{
    return random() * (max - min) + min
}

该游戏在生成我需要的节点方面运行良好,但是冻结确实杀死了游戏。非常感谢我已经为此工作了几天,并希望继续前进。谢谢。

0 个答案:

没有答案