我一直致力于一项涉及SpriteKit快速移动的游戏。我已经对游戏进行了测试和分析,删除了游戏的多个部分,并且无法保持稳定的60 fps。帧速率下降导致节点移动到断断续续。我决定建立一个简单的程序进行测试。屏幕上只有4个节点从左向右移动,iPhone 7 Plus上运行的帧数仍有下降。有没有办法优化SpriteKit中的快速移动以保持稳定的帧速率?
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var ship = SKSpriteNode()
var ship2 = SKSpriteNode()
var ship3 = SKSpriteNode()
var ship4 = SKSpriteNode()
override func didMove(to view: SKView) {
ship.texture = SKTexture(imageNamed: "Spaceship")
ship.position = CGPoint(x: -300, y: 0)
ship.size = CGSize(width: 50, height: 50)
ship.setScale(1)
ship.physicsBody = SKPhysicsBody(rectangleOf: ship.size)
ship.physicsBody?.affectedByGravity = false
ship.zPosition = 2
self.addChild(ship)
ship2.texture = SKTexture(imageNamed: "Spaceship")
ship2.position = CGPoint(x: -200, y: -50)
ship2.size = CGSize(width: 50, height: 50)
ship2.setScale(1)
ship2.physicsBody = SKPhysicsBody(rectangleOf: ship.size)
ship2.physicsBody?.affectedByGravity = false
ship2.zPosition = 2
self.addChild(ship2)
ship3.texture = SKTexture(imageNamed: "Spaceship")
ship3.position = CGPoint(x: -100, y: 50)
ship3.size = CGSize(width: 50, height: 50)
ship3.setScale(1)
ship3.physicsBody = SKPhysicsBody(rectangleOf: ship.size)
ship3.physicsBody?.affectedByGravity = false
ship3.zPosition = 2
self.addChild(ship3)
ship4.texture = SKTexture(imageNamed: "Spaceship")
ship4.position = CGPoint(x: -0, y: 100)
ship4.size = CGSize(width: 50, height: 50)
ship4.setScale(1)
ship4.physicsBody = SKPhysicsBody(rectangleOf: ship.size)
ship4.physicsBody?.affectedByGravity = false
ship4.zPosition = 2
self.addChild(ship4)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
ship.position.x += 15
ship2.position.x += 15
ship3.position.x += 15
ship4.position.x += 15
if ship.position.x > 800 {
ship.position.x = -950
} else if ship2.position.x > 800 {
ship2.position.x = -900
} else if ship3.position.x > 800 {
ship3.position.x = -850
} else if ship4.position.x > 800 {
ship4.position.x = -800
}
}
}
编辑:在分析测试程序时,帧速率降至59 fps。