Swift游戏在创建spritenodes时滞后

时间:2015-12-13 21:19:03

标签: ios swift

因此,我最近将一款游戏上传到了应用商店,并且一直是用户报告旧设备上的延迟。 首先,游戏: 它被称为" CURVE" (我不确定我是否可以在这里发布它的名字,但它会帮助你理解这个问题。)游戏的想法是一个球在屏幕上移动并穿过墙壁之间的间隙。还有较小的球从主球落下,从而形成某种路径。 现在,滞后。它们发生在两个主要时刻。首先,当创建路径时,第二,当玩家通过间隙时。 当玩家通过间隙时,他会与一个不可见的节点发生碰撞。注意到collsion,分数更新。然后移除该节点并且播放器继续。 我相信当swift创建SpriteNode或删除一个SpriteNode时会出现延迟。关于如何处理这个问题的任何想法?

这是我在产生障碍时使用的代码 - 我认为问题出在哪里

func createRocks() {

    rockHeight = (frame.height * (heightOfRocks))

    if gameState != .Dead {

    switch spawnCount {

    case 10:
        levelUp()

    case 20:
        levelUp()

    case 30:
        levelUp()

    case 40:
        levelUp()

    case 50:
        levelUp()

    case 60:
        levelUp()

    case 70:
        levelUp()

    case 80:
        levelUp()

    case 90:
        levelUp()

    case 100:
        levelUp()

    case 110:
        levelUp()

    case 120:
        levelUp()

    case 130:
        levelUp()

    case 140:
        levelUp()

    case 150:
        levelUp()

    default:
        break

    }
    }
    ++spawnCount






    let leftRock = SKShapeNode(rectOfSize: CGSize(width: (frame.width), height: (rockHeight)), cornerRadius: (cornerRad))
    leftRock.fillColor = themeColor
    leftRock.strokeColor = themeBorderColor
    leftRock.lineWidth = themeBorderWidth

    leftRock.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: ((frame.width) + (themeBorderWidth)), height: ((rockHeight) + (themeBorderWidth))))



    leftRock.physicsBody?.dynamic = false



    let rightRock = SKShapeNode(rectOfSize: CGSize(width: (frame.width), height: (rockHeight)), cornerRadius: (cornerRad))
    rightRock.fillColor = themeColor
    rightRock.strokeColor = themeBorderColor
    rightRock.lineWidth = (themeBorderWidth)


    rightRock.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: ((frame.width) + (themeBorderWidth)), height: ((rockHeight) + (themeBorderWidth))))
    rightRock.physicsBody?.dynamic = false


    leftRock.zPosition = CGFloat(10 + (21 * levelNumber))
    rightRock.zPosition = CGFloat(10 + (21 * levelNumber))


    // 2
    let rockCollision = SKSpriteNode(color: UIColor.clearColor(), size: CGSize(width: (frame.width * 2), height: 32))

    rockCollision.physicsBody = SKPhysicsBody(rectangleOfSize: rockCollision.size)
    rockCollision.physicsBody?.dynamic = false

    rockCollision.name = "scoreDetect"

    addChild(leftRock)
    addChild(rightRock)
    addChild(rockCollision)



    // 3

    let rockWidth = frame.width

    let yPosition = frame.height + leftRock.frame.height

    let rockDistance = frame.width * (gapWidth)

    let min = Int(((frame.width) * (wallWidth)) - (rockWidth / 2))
    let max = Int(((frame.width) * (1.00 - ((wallWidth)))) - rockDistance - (rockWidth / 2))


    let rand = min + Int(arc4random_uniform(UInt32(max - min)))
    //GKRandomDistribution(lowestValue: min, highestValue: max)

    let xPosition = CGFloat(rand)



    // 4
    leftRock.position = CGPoint(x: xPosition, y: yPosition)
    rightRock.position = CGPoint(x: xPosition + rockWidth + rockDistance, y: yPosition)


    rockCollision.position = CGPoint(x: xPosition, y: CGRectGetMidY(frame))

    rockCollision.position = CGPoint(x: CGRectGetMidX(frame), y: yPosition + (rockCollision.size.height * 2))





    let endPosition = frame.height + (leftRock.frame.height * 2)



    let moveAction = SKAction.moveByX(0, y: -endPosition, duration: timeOfRockMovement)
    let moveSequence = SKAction.sequence([moveAction, SKAction.removeFromParent()])
    leftRock.runAction(moveSequence)
    rightRock.runAction(moveSequence)
    rockCollision.runAction(moveSequence)
}
func initRocks() {



    let create = SKAction.runBlock { [unowned self] in
        self.createRocks()
    }

    let wait = SKAction.waitForDuration(rockSpawnWait)
    let sequence = SKAction.sequence([create, wait])
    let repeatForever = SKAction.repeatActionForever(sequence)

    runAction(repeatForever)
}

1 个答案:

答案 0 :(得分:0)

你的方法听起来与a SpriteKit tutorial I wrote非常相似,但我根本没有任何延迟。 SpriteKit速度非常快,但您的确切条件可能会导致问题。要检查的事项:

1)你在使用纹理地图集吗?加载速度比捆绑中的单独图像更快。

2)如果您重复使用图片,可以将它们存储在SKTexture中,然后从那里加载节点吗?

3)是否还有其他事情发生,比如声音在播放? iOS模拟器在游戏中播放第一个声音时经常会出现短暂的延迟。

4)创建的路径是否非常复杂?