Claudio D'Agostino
Claudio D'Agostino

Reputation: 115

SpriteKit game lag when the function touchesBegan is called

I'm creating my first game, similar to flappy bird. I want it to start when the screen is touched like the real game. But it lags for about a second and an half, the result is that you die without even playing. Here is my code:

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()
        }
    }

Pipes init:

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)


}

It's pretty simple: in initPipes() i create the pipes and i run the action of moving and removing. In touchesBegan i call the action of spawning them.. But it's laggy when i touch the screen.

Upvotes: 1

Views: 615

Answers (1)

Travis
Travis

Reputation: 3369

Run your app through the Time Profiler Instrument to find out where the lag is coming from. It'll give you detailed results (down to individual lines of code) that'll let you know where your issues are.

That'll be much more accurate than people on here guessing.

Upvotes: 2

Related Questions