调用函数touchesBegan时SpriteKit游戏滞后

时间:2015-07-17 17:58:03

标签: swift sprite-kit

我正在制作我的第一款游戏,类似于飞扬的小鸟。我希望它能像真实游戏一样触摸屏幕时启动。但它滞后大约一秒半,结果是你甚至没有玩就会死。这是我的代码:

override func didMoveToView(view: SKView) {

    /* Setup your scene here */
   //Here i init some stuff

    distanceToMove = CGFloat(self.frame.size.width + 140)
    movePipes = SKAction.repeatActionForever(SKAction.moveByX(-distanceToMove, y: 0, duration: NSTimeInterval(1.2)))
    removePipes = SKAction.removeFromParent()
    moveAndRemove = SKAction.sequence([movePipes,removePipes])


    let delay = SKAction.waitForDuration(NSTimeInterval(0.6))
    let spawn = SKAction.runBlock({() in self.initPipes()})
    let spawnAndDelay = SKAction.sequence([spawn,delay])
    spawnAndDelayForever = SKAction.repeatActionForever(spawnAndDelay)


}

的touchesBegan:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */
    let touch = touches.first! as UITouch!
    let touchLocation = touch.locationInNode(self)

    if state == GameState.Starting {

        state = GameState.Playing

        instNode.hidden = true

        if state == GameState.Playing {
            runAction(spawnAndDelayForever)
            addChild(pipes)
            initScore()
        }
    }

管道初始化:

func initPipes() {
    let upper = UInt32(self.size.height - 250)
    let pY = arc4random_uniform(upper) + 200
    let pipePair = SKNode()
    pipePair.position = CGPoint(x: self.frame.size.width + 70, y: 0)
    //PIPE 1
    let pipe1 = SKSpriteNode(color: SKColor.whiteColor(), size: CGSizeMake(70, 700))

    pipe1.anchorPoint = CGPointMake(0, 0)
    pipe1.position = CGPoint(x: 0, y: Int(pY))
    pipe1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(70, 700), center: CGPointMake(70/2, 700/2))
    pipe1.physicsBody?.dynamic = false
    pipe1.physicsBody?.affectedByGravity = false
    pipe1.physicsBody?.categoryBitMask = PipeCategory
    pipe1.physicsBody?.contactTestBitMask = PlayerCategory
    pipe1.physicsBody?.collisionBitMask = PlayerCategory
    pipePair.addChild(pipe1)

    //PIPE 2
    let pipe2 = SKSpriteNode(color: SKColor.whiteColor(), size: CGSizeMake(70, 700))

    pipe2.anchorPoint = CGPointMake(0,1)
    pipe2.position = CGPoint(x: 0, y: pipe1.position.y - 150)
    pipe2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(70, 700), center: CGPointMake(35, -700/2))
    pipe2.physicsBody?.dynamic = false
    pipe2.physicsBody?.affectedByGravity = false
    pipe2.physicsBody?.categoryBitMask = PipeCategory
    pipe2.physicsBody?.contactTestBitMask = PlayerCategory
    pipe2.physicsBody?.collisionBitMask = PlayerCategory
    pipePair.addChild(pipe2)


    //SCORE
    let scoreSprite = SKSpriteNode(color: SKColor.clearColor(), size: CGSize(width: 50, height: 150))
    scoreSprite.anchorPoint = CGPointMake(0, 1)
    scoreSprite.position = CGPointMake(pipe1.position.x + 10, pipe1.position.y)
    scoreSprite.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 50, height: 150), center: CGPointMake(25, -75))
    scoreSprite.physicsBody?.categoryBitMask = GapCategory
    scoreSprite.physicsBody?.contactTestBitMask = PlayerCategory
    scoreSprite.physicsBody?.collisionBitMask = 0
    scoreSprite.physicsBody?.dynamic = false
    pipePair.addChild(scoreSprite)


    pipePair.runAction(moveAndRemove)
    pipes.addChild(pipePair)


}

这很简单:在initPipes()中我创建管道,然后运行移动和移除操作。在touchesBegan中,我称之为产生它们的动作..但是当我触摸屏幕时它很迟钝。

1 个答案:

答案 0 :(得分:2)

通过Time Profiler Instrument运行您的应用,找出延迟的来源。它会为您提供详细的结果(直到各行代码),以便您了解问题所在。

这比那些猜测的人准确得多。