Hello,
My game app is having FPS issues, both on simulator and device.
There aren't any issues until my sknodes get added to the scene. I've read plenty of forums and as of yet nothing seems to work.
Ive checked my performance and the memory isn't particularly high.
Any help would be massively appreciated.
Thank you!
My game app is having FPS issues, both on simulator and device.
There aren't any issues until my sknodes get added to the scene. I've read plenty of forums and as of yet nothing seems to work.
Ive checked my performance and the memory isn't particularly high.
Any help would be massively appreciated.
Thank you!
Code Block import SpriteKit import GameplayKit class GameScene: SKScene, SKPhysicsContactDelegate { let player = SKSpriteNode(imageNamed: "Player") var touchingPlayer = false var gameTimer = Timer() let scoreLabel = SKLabelNode(fontNamed: "AvenirNextCondensed-Bold") var score = 0 { didSet { scoreLabel.text = "SCORE: \(score)" } } override func didMove(to view: SKView) { let background = SKSpriteNode(imageNamed: "Background.png") background.zPosition = -1 background.size = CGSize(width: 1334, height: 750) background.position.x = 0 addChild(background) scoreLabel.zPosition = 2 scoreLabel.position.y = 100 addChild(scoreLabel) score = 0 player.position.x = -200 player.zPosition = 1 player.name = "Player" player.size = CGSize(width: 70, height: 70) addChild(player) gameTimer = Timer.scheduledTimer(timeInterval: 0.35, target: self, selector: #selector(createEnemy), userInfo: nil, repeats: true) player.physicsBody = SKPhysicsBody(texture: player.texture!, size: player.size) player.physicsBody?.categoryBitMask = 1 player.physicsBody?.affectedByGravity = false physicsWorld.contactDelegate = self } func touchDown(atPoint pos : CGPoint) { } func touchMoved(toPoint pos : CGPoint) { } func touchUp(atPoint pos : CGPoint) { } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { guard let touch = touches.first else {return} let location = touch.location(in: self) let tappedNodes = nodes(at: location) if tappedNodes.contains(player) { touchingPlayer = true } } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { guard touchingPlayer else {return} guard let touch = touches.first else {return} let location = touch.location(in: self) player.position = location } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { touchingPlayer = false } override func update(_ currentTime: TimeInterval) { for node in children { if node.position.x < -700 { node.removeFromParent() } } // if player.position.x < -300 { // player.position.x = -300 // } else if player.position.x > 300 { // player.position.x = 300 // } // // if player.position.y < -200 { // player.position.y = -200 // } else if player.position.y > 200 { // player.position.y = 200 // } } @objc func createEnemy(){ let randomDistribution = GKRandomDistribution(lowestValue: -350, highestValue: 350) let sprite = SKSpriteNode(imageNamed: "Virus") sprite.position = CGPoint(x: 700, y: randomDistribution.nextInt()) sprite.name = "Virus" sprite.zPosition = 1 sprite.size = CGSize(width: 70, height: 70) addChild(sprite) sprite.physicsBody = SKPhysicsBody(texture: sprite.texture!, size: sprite.size) sprite.physicsBody?.velocity = CGVector(dx: -500, dy: 0) sprite.physicsBody?.linearDamping = 0 sprite.physicsBody?.contactTestBitMask = 1 sprite.physicsBody?.categoryBitMask = 0 sprite.physicsBody?.affectedByGravity = false createBonus() } func createBonus(){ let randomDistribution = GKRandomDistribution(lowestValue: -350, highestValue: 350) let sprite = SKSpriteNode(imageNamed: "Vaccine") sprite.position = CGPoint(x: 700, y: randomDistribution.nextInt()) sprite.name = "Vaccine" sprite.size = CGSize(width: 70, height: 70) sprite.zPosition = 1 addChild(sprite) sprite.physicsBody = SKPhysicsBody(texture: sprite.texture!, size: sprite.size) sprite.physicsBody?.velocity = CGVector(dx: -500, dy: 0) sprite.physicsBody?.linearDamping = 0 sprite.physicsBody?.contactTestBitMask = 1 sprite.physicsBody?.categoryBitMask = 0 sprite.physicsBody?.collisionBitMask = 0 sprite.physicsBody?.affectedByGravity = false } func didBegin(_ contact: SKPhysicsContact) { guard let nodeA = contact.bodyA.node else {return} guard let nodeB = contact.bodyB.node else {return} if nodeA == player { playerHit(nodeB) } else { playerHit(nodeA) } } func playerHit(_ node: SKNode) { if node.name == "Virus" { if let particles = SKEmitterNode(fileNamed: "dead.sks") { particles.position = player.position particles.zPosition = 3 addChild(particles) } player.removeFromParent() } else if node.name == "Vaccine" { if player.parent != nil { score += 1 } node.removeFromParent() return } DispatchQueue.main.asyncAfter(deadline: .now() + 3) { if let scene = GameScene(fileNamed: "GameScene") { scene.scaleMode = .aspectFill self.view?.presentScene(scene) } } } }