I'm developing a simple game with Swift & SpriteKit, and I've noticed FPS drops from 60 to 58-59 (and back). There is a noticeable lag when the drop occurs — it looks like 1 or 2 frames are dropped.
CPU load is about 20-25% and does not change a lot, memory usage is permanently about 8 MB.
There are 6 object on screen: label, red object (Sprite), 2x green objects (Sprite), one box (Sprite) and "ground" (Rect shape node).
All objects except label have physics body (displayed with white borders).
Green and white objects are dynamically created, move from right to left and destroyed when offscreen:
func scheduleAddingLiquid() {
let wait = SKAction.waitForDuration(NSTimeInterval(getRandomNumber(1, end: 3)))
let block = SKAction.runBlock({
[unowned self] in
let liquidNode = LiquidNode(texture: self.liquidTexture, sceneFrame: self.frame)
self.addChild(liquidNode)
liquidNode.move()
self.scheduleAddingLiquid()
})
let sequence = SKAction.sequence([wait, block])
runAction(sequence)
and:
func move() {
let moveAction = SKAction.moveToX(-frame.width, duration: NSTimeInterval(3))
let removeBlock = SKAction.runBlock({
[unowned self] in
self.removeFromParent()
})
runAction(SKAction.sequence([moveAction, removeBlock]))
}
Red object "jumps" on screen touch:
if touches.count > 0 && isHeroOnGround && heroNode != nil {
isHeroOnGround = false
heroNode.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
heroNode.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 400))
}
The lag occurs within random time interval after "jumping" (from about 0.5 to 1.5 seconds after jump).
The lag occurs not when collision occurs, just when red object is "in the air". And it occurs not every jump. The CPU load does not grow when FPS drops.
Tested on iOS 9.3, iPad mini 2.
Upd 1. Tested on iOS 9.3 iPhone 6 — FPS is about 50-55 for first few seconds, then it's constantly 60, without lags. So, it lags on iPad mini 2 only (I have only these two devices, cannot test on other ones).
UPD 2. I've commented out all objects creation code, except red figure. It's still lagging when "jumping". So, now I'm sure that it's related to applyImpulse method.
UPD 3. That's really, REALLY strange: I've removed all code from touchesBegan method, and it still lags on touches! The method is completely empty. I don't know why, but FPS drops if I click several times...
How to debug this?