I'm creating a game using Sprite Kit, and I have an issue where the game slowly loses fps with every reiteration of the my main scene.
I've tried to use Xcode's Instruments, but have gotten no closer to the root of my issue.
This is my function to end the game and present the Game Over Scene:
func endGame(){
let endGameScene = SKScene(fileNamed: "EndGameScene" ) as! EndGameScene
endGameScene.score = score
//let transition = SKTransition.push(with: SKTransitionDirection.right, duration: 0.5)
self.view?.presentScene(endGameScene)
transitionNode = SKSpriteNode(color: UIColor.white, size: self.frame.size)
transitionNode.zPosition = 5
self.addChild(transitionNode)
transitionNode.run(SKAction.fadeIn(withDuration: 0.3))
gameOver = true
}
This is my function to present the Game Scene as a result of the
restart button being tapped:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in: self) {
let nodeArray = self.nodes(at: location)
if(nodeArray.first?.name == "replayButton"){
self.view?.presentScene(gameScene)
transitionNode = SKSpriteNode(color: UIColor.white, size: self.frame.size)
transitionNode.zPosition = 5
self.addChild(transitionNode)
transitionNode.run(SKAction.fadeIn(withDuration: 0.3))
I also checked for leaks using instruments, but none come up. Similarly, the heaviest stack trace log shows no areas in my class file that took the most time, as shown here: (wasn't able to upload an image for some reason, so here's the link)
https://puu.sh/wtrMZ/1bb8c6542a.png
The game starts at a solid 60 fps, and then drops to 58, then to 57, then to 55, then to 54, and so on. I tried the deinit {} test, and the scene deallocates fine. I used the debugger to check my memory usage, and while it does go up ever so slightly, it barely breaks 50 mb in a single sitting. I also don't think it's a problem of removing unused nodes, because I make sure to remove them in every instance, but I could be wrong:
deinit
deinit {
self.removeAllChildren()
}
I am genuinely stumped, as I don't even know how to begin to fix this. Any help or constructive criticism is very much appreciated, thanks.
EDIT:
I never invalidated the two main timers in my Game Scene class. I simply invalidated them in my endGame function:
`func endGame(){
let endGameScene = SKScene(fileNamed: "EndGameScene" ) as! EndGameScene
endGameScene.score = score
//let transition = SKTransition.push(with: SKTransitionDirection.right, duration: 0.5)
self.view?.presentScene(endGameScene)
transitionNode = SKSpriteNode(color: UIColor.white, size: self.frame.size)
transitionNode.zPosition = 5
self.addChild(transitionNode)
transitionNode.run(SKAction.fadeIn(withDuration: 0.3))
pillarTimer.invalidate()
durationTimer.invalidate()
gameOver = true
}`
Fixed my issue perfectly.
Related
I am pretty new to SpriteKit so I may be missing something quite obvious.
I am attempting to create an interactive map of the US. I have loaded PNG images for each state and placed a couple of them into the main SKScene using the scene editor.
My goal is wanting to detect when each state is tapped by the user. I initially wrote some code that would find the nodes within a touch location however, this meant that the user could tap the frame and it would be counted as a valid tap. I only want to register touches that happen within the state texture and not the frame. Reading online it was suggested to use SKPhysicsBody to register when a tap takes place. So I changed my code to the following.
class GameScene: SKScene {
override func didMove(to view: SKView) {}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let location: CGPoint = self.convertPoint(fromView: touch.location(in: self.view))
let body = self.physicsWorld.body(at: location)
if let state = body?.node, let name = state.name {
state.run(SKAction.run({
var sprite = self.childNode(withName: name) as! SKSpriteNode
sprite.color = UIColor.random()
sprite.colorBlendFactor = 1
}))
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
Now, if I choose the Bounding circle body type everything works as expected (shown above in the screenshot). When I click within the boudning circle it runs the SKAction otherwise it does nothing. However, when I change the body type to Alpha mask (the body type I want) it suddenly stops detecting the state. In fact, it returns the SKPhysicsBody for the MainScene entity.
Any advice on how to fix this would be greatly appreciated.
Thanks.
i can reproduce this behavior (bug?) when using the scene editor. however it goes away if you skip the sks file and initialize your sprites in code. (i acknowledge that setting locations for 50 states is more tedious this way.)
class GameScene: SKScene {
let ok = SKSpriteNode(imageNamed: "ok")
let nm = SKSpriteNode(imageNamed: "nm")
let tx = SKSpriteNode(imageNamed: "tx")
override func didMove(to view: SKView) {
tx.name = "Texas"
nm.name = "New Mexico"
ok.name = "Oklahoma"
//set up physics bodies w/ alpha mask
tx.physicsBody = SKPhysicsBody(texture: tx.texture!, size: tx.texture!.size())
tx.physicsBody?.affectedByGravity = false
nm.physicsBody = SKPhysicsBody(texture: nm.texture!, size: nm.texture!.size())
nm.physicsBody?.affectedByGravity = false
ok.physicsBody = SKPhysicsBody(texture: ok.texture!, size: ok.texture!.size())
ok.physicsBody?.affectedByGravity = false
//then position your sprites and add them as children
}
}
I have been following an old Ray Wenderlich tutorial: https://www.raywenderlich.com/1514-introduction-to-the-sprite-kit-scene-editor and have encountered a problem. I have run up against this problem numerous times and even found a "solution" on the stackoverflow site: Passing Data Between Scenes (SpriteKit)
This is the same code as the tutorial (with a few tweaks as it was written in Swift 2). I feel it a very simply elegant solution and not full of code using NSdefaults etc.
This is the call to the new scene, please note the soundToPlay object:
func gameOver(didWin: Bool) {
let menuScene = MenuScene(size: self.size)
menuScene.scaleMode = .aspectFill
menuScene.soundToPlay = didWin ? "fear_win.mp3" : "fear_lose.mp3"
let transition = SKTransition.flipVertical(withDuration: 1.0)
self.view?.presentScene(menuScene, transition: transition)
}
And this is the scene I am calling:
import SpriteKit
class MenuScene: SKScene {
var soundToPlay: String!
override func sceneDidLoad() {
self.backgroundColor = SKColor(red: 0, green:0, blue:0, alpha: 1)
// Setup label
let label = SKLabelNode(fontNamed: "AvenirNext-Bold")
label.text = "Press anywhere to play again!"
label.fontSize = 55
label.horizontalAlignmentMode = .center
label.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
addChild(label)
// Play sound
if let soundToPlay = soundToPlay {
run(SKAction.playSoundFileNamed(soundToPlay, waitForCompletion: false))
}
}
The data is never passed and the object has a value of nil
I suspect it's something that has changed in Swift as the tutorial is very old as is the suggested "solution" on the previous stackoverflow question.
If that is the case, what is now the best way to achieve this?
The problem with your sound is that you are setting the variable after you call sceneDidLoad (this happens during init), so the sound will not play.
To fix this, do this code in your didMove method:
override func didMove(to view: SKView) {
guard let soundToPlay = soundToPlay else {fatalError("Unable to find soundToPlay")}
run(SKAction.playSoundFileNamed(soundToPlay, waitForCompletion: false))
}
The actual issue to your crashing is not visible here.
SKTransitions are bugged when you use SKLightNode, so by calling a scene with a transition, it is going to crash. SKTransitions have always been bugged with the introduction of Metal, so the only thing you can do is report it to apple and pray they do not tell you it is intentional.
As of now, keep track of all your light sources, and when you transition, set the isEnabled property to false.
You may need to create a screen shot of your scene prior to disabling the light source to not have it look silly.
What you are doing is, as you reload the scene (let menuScene = MenuScene(size: self.size)) you are resetting all the variables that were defined.
What you could do is use UserDefaults:
func gameOver(didWin: Bool) {
let menuScene = MenuScene(size: self.size)
menuScene.scaleMode = .aspectFill
let soundToPlay = didWin ? "fear_win.mp3" : "fear_lose.mp3"
UserDefaults.standard.set(soundToPlay, forKey: "soundToPlay")
let transition = SKTransition.flipVertical(withDuration: 1.0)
self.view?.presentScene(menuScene, transition: transition)
}
And:
import SpriteKit
class MenuScene: SKScene {
var soundToPlay: String!
override func sceneDidLoad() {
self.backgroundColor = SKColor(red: 0, green:0, blue:0, alpha: 1)
// Setup label
let label = SKLabelNode(fontNamed: "AvenirNext-Bold")
label.text = "Press anywhere to play again!"
label.fontSize = 55
label.horizontalAlignmentMode = .center
label.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
addChild(label)
// Play sound
if let soundToPlay = UserDefaults.standard.string(forKey: "soundToPlay") {
run(SKAction.playSoundFileNamed(soundToPlay, waitForCompletion: false))
}
}
Then, put this in your AppDelegate's applicationWillTerminate function:
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
UserDefaults.standard.removeObject(forKey: "soundToPlay")
}
If you do not put that in the AppDelegate then soundToPlay will remain after closing the app altogether.
I don't know if UserDefaults is the best way to do this, but I've struggled with this when I was making my iOS app and I just did this. So if anyone else has a better answer, please notify me as well!
PLEASE HELP!!! I have been trying to figure this out for along time. I have searched the internet and i cannot find anything that will help me.
I am currently making a game in which you are a space ship in the middle and enemy ships are moving towards you and you have to shoot them. some enemies have different lives. for example: a red ship takes one shot to explode, the blue ship takes 3, etc. I have everything to work only the lives. for example: whenever a blue ship is called on to the screen i shoot it once so its life goes down to 2. but whenever a another blue ship is called the first blue ship has its life reset back to 3 again. Is there anyway I can make it so that whenever a ship looses lives it remains that way even if other ships are called ?
this is my ship function that gets called and adds enemy space ships onto the screen:
func VillainRight(){
let TooMuch = self.size.width
let point = UInt32(TooMuch)
life = 3
let VillainR = SKSpriteNode(imageNamed: "BlueVillain")
VillainR.zPosition = 2
VillainR.position = CGPoint(x: self.frame.minX,y: CGFloat(arc4random_uniform(point)))
//This code makes the villain's Zposition point towards the SpaceShip
let angle = atan2(SpaceShip.position.y - VillainR.position.y, SpaceShip.position.x - VillainR.position.x)
VillainR.zRotation = angle - CGFloat(M_PI_2)
let MoveToCenter = SKAction.move(to: CGPoint(x: self.frame.midX, y: self.frame.midY), duration: 15)
//Physics World
VillainR.physicsBody = SKPhysicsBody(rectangleOf: VillainR.size)
VillainR.physicsBody?.categoryBitMask = NumberingPhysics.RightV
VillainR.physicsBody?.contactTestBitMask = NumberingPhysics.Laser | NumberingPhysics.SpaceShip
VillainR.physicsBody?.affectedByGravity = false
VillainR.physicsBody?.isDynamic = true
VillainR.run(MoveToCenter)
addChild(VillainR)
}
This is the code that calls this function:
_ = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(Level1.VillainRight), userInfo: nil, repeats: true)
I am using the spritekit in Swift.
Thank You Very Much in advance!
That is happening because life variable is declared as a property of a scene and it is not local to a specific node (enemy ship). You can solve this in a few ways... First way would be using node's userData property:
import SpriteKit
let kEnergyKey = "kEnergyKey"
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMove(to view: SKView) {
let blueShip = getShip(energy: 3)
let greenShip = getShip(energy: 2)
let redShip = getShip(energy: 1)
if let blueShipEnergy = blueShip.userData?.value(forKey: kEnergyKey) as? Int {
print("Blue ship has \(blueShipEnergy) lives left")
//hit the ship
blueShip.userData?.setValue(blueShipEnergy-1, forKey: kEnergyKey)
if let energyAfterBeingHit = blueShip.userData?.value(forKey: kEnergyKey) as? Int {
print("Blue ship has \(energyAfterBeingHit) lives left")
}
}
}
func getShip(energy:Int)->SKSpriteNode{
//determine which texture to load here based on energy value
let ship = SKSpriteNode(color: .purple, size: CGSize(width: 50, height: 50))
ship.userData = [kEnergyKey:energy]
return ship
}
}
This is what the docs say about userData property:
You use this property to store your own data in a node. For example,
you might store game-specific data about each node to use inside your
game logic. This can be a useful alternative to creating your own node
subclasses to hold game data.
As you can see, an alternative to this is subclassing of a node (SKSpriteNode):
class Enemy:SKSpriteNode {
private var energy:Int
//do initialization here
}
I'm using Swift 3.0, SpriteKit, and Xcode 8.2.1, testing on an iPhone 6s running iOS 10.2.
The problem is simple... on the surface. Basically my TouchesMoved() updates at a very inconsistent rate and is destroying a fundamental part of the UI in my game. Sometimes it works perfectly, a minute later it's updating at half of the rate that it should.
I've isolated the problem. Simply having an SKSpriteNode in the scene that has a physics body causes the problem... Here's my GameScene code:
import SpriteKit
import Darwin
import Foundation
var spaceShip = SKTexture(imageNamed: "Spaceship")
class GameScene: SKScene{
var square = SKSpriteNode(color: UIColor.black, size: CGSize(width: 100,height: 100))
override func didMove(to view: SKView) {
backgroundColor = SKColor.white
self.addChild(square)
//This is what causes the problem:
var circleNode = SKSpriteNode(texture: spaceShip, color: UIColor.clear, size: CGSize(width: 100, height: 100))
circleNode.physicsBody = SKPhysicsBody(circleOfRadius: circleNode.size.width/2)
self.addChild(circleNode)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
var positionInScreen = touch.location(in: self)
square.position = positionInScreen
}
}
}
The problem doesn't always happen so sometimes you have to restart the app like 5 times, but eventually you will see that dragging the square around is very laggy if you do it slowly. Also I understand it's subtle at times, but when scaled up this is a huge problem.
My main question:
Why does me having an SKSpriteNode with a physics body cause TouchesMoved() to lag and nothing else to lag, and how can I prevent this?
Please for the love of code and my sanity save me from this abyss!!!
It looks like this is caused by the OS being too busy to respond to touch events. I found two ways to reproduce this:
Enable Airplane Mode on the device, then disable it. For the ~5-10 seconds after disabling Airplane Mode, the touch events lag.
Open another project in Xcode and select "Wait for the application to launch" in the scheme editor, then press Build and Run to install the app to the device without running it. While the app is installing, the touch events lag.
It doesn't seem like there is a fix for this, but here's a workaround. Using the position and time at the previous update, predict the position and time at the next update and animate the sprite to that position. It's not perfect, but it works pretty well. Note that it breaks if the user has multiple fingers on the screen.
class GameScene: SKScene{
var lastTouchTime = Date.timeIntervalSinceReferenceDate
var lastTouchPosition = CGPoint.zero
var square = SKSpriteNode(color: UIColor.black, size: CGSize(width: 100,height: 100))
override func didMove(to view: SKView) {
backgroundColor = SKColor.white
self.addChild(square)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
lastTouchTime = Date().timeIntervalSinceReferenceDate
lastTouchPosition = touches.first?.location(in: self) ?? .zero
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let currentTime = Date().timeIntervalSinceReferenceDate
let timeDelta = currentTime - lastTouchTime
for touch in touches{
square.removeAction(forKey: "TouchPrediction")
let oldPosition = lastTouchPosition
let positionInScreen = touch.location(in: self)
lastTouchPosition = positionInScreen
square.position = positionInScreen
//Calculate the difference between the sprite's last position and its current position,
//and use it to predict the sprite's position next frame.
let positionDelta = CGPoint(x: positionInScreen.x - oldPosition.x, y: positionInScreen.y - oldPosition.y)
let predictedPosition = CGPoint(x: positionInScreen.x + positionDelta.x, y: positionInScreen.y + positionDelta.y)
//Multiply the timeDelta by 1.5. This helps to smooth out the lag,
//but making this number too high cause the animation to be ineffective.
square.run(SKAction.move(to: predictedPosition, duration: timeDelta * 1.5), withKey: "TouchPrediction")
}
lastTouchTime = Date().timeIntervalSinceReferenceDate
}
}
I had similar issues when dragging around an image using the touchesMoved method. I was previously just updating the node's position based on where the touch was, which was making the movement look laggy. I made it better like this:
//in touchesMoved
let touchedPoint = touches.first!
let pointToMove = touchedPoint.location(in: self)
let moveAction = SKAction.move(to: pointToMove, duration: 0.01)// play with the duration to get a smooth movement
node.run(moveAction)
Hope this helps.
I want a game over screen and the game to stop when the screen is touched during a specific animation.
let lightTexture = SKTexture(imageNamed: "green light.png")
let lightTexture2 = SKTexture(imageNamed: "red light.png")
let animateGreenLight = SKAction.sequence([SKAction.waitForDuration(2.0, withRange: 0.1), SKAction.animateWithTextures([lightTexture, lightTexture2], timePerFrame: 3)])
let changeGreenLight = SKAction.repeatActionForever(animateGreenLight)
let animateRedLight = SKAction.sequence([SKAction.waitForDuration(2.0, withRange: 0.1), SKAction.animateWithTextures([lightTexture, lightTexture2], timePerFrame: 3)])
let changeRedLight = SKAction.repeatActionForever(animateRedLight)
let greenLight = SKSpriteNode(texture: lightTexture)
greenLight.position = CGPointMake(CGRectGetMidX(self.frame), 650)
greenLight.runAction(changeGreenLight)
self.addChild(greenLight)
let redLight = SKSpriteNode(texture: lightTexture2)
redLight.position = CGPointMake(CGRectGetMidX(self.frame), 650)
redLight.runAction(changeRedLight)
self.addChild(redLight)
When the animation for the red light is on the screen, I want it to be game over. Do I have to make an if statement, and if so for what specifically?
Thank You in advance!
You can make yourself another node which has the same size and position as the red light. Additionally, that node is able to handle touch events. Then, add that node before the animation runs. This can be done with a sequence of actions, e.g.:
let addAction = SKAction.runBlock({ self.addChild(touchNode) })
let animationAction = SKAction.repeatActionForever(animateRedLight)
redLight.runAction(SKAction.sequence([ addAction, animationAction ]))
Update
Since you want the game to end when you touch anywhere, alter the code such that the block sets a variable which indicates that the animation is executed and implement touchesBegan which checks for that variable, e.g.
let addAction = SKAction.runBlock({ self.redLightAnimationRuns = true })
[...]
// In touchesBegan
if touched
{
if redLightAnimationRuns
{
endGame()
}
}
use the touchesBegan() function to call a GameOver() function when the red light is on the screen (which you can control with a variable).
So, when the red light comes on to the screen, variable redLightCurrent is set to true. in TouchesBegan(), when redLightCurrent is true, then call the gameOver() function where you can include what to do when the game is over. This will only occur when a touch has began on the screen.
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
let array = Array(touches)
let touch = array[0] as UITouch
let touchLocation = touch.locationInNode(self)
if redLightCurrent {
gameOver()
}
}
This code works with the new xCode 7 and Swift 2.0