Sprite kit sprites loading performance improvement - ios

I am making a sprite kit game and I am using the plist file to set properties of each level. One of the properties in my plist file is a dictionary called patterns, which contains n items, where each of the items is a block, with hand typed x and y positions. This model is working perfectly fine for the kind of game I am making, as it is very convenient to set the levels right in a quick manner. However, I am facing one drawback I cant solve myself due to the lack of coding experience: some of the levels have as many as 290 blocks, so when the app tries to read the level, the app freezes for like 5 seconds. This is very annoying for the user. At the beginning my approach was: Read the plist file, and for each item call the method which creates the block as a SKSpriteNode using its imageNamed "" method. I thought this is the reason it lags so much, the fact that I am trying to load 300 sprites at the runtime seemed as a promising cause of the problem. Then I tried the following: I made the method which loads a pool of block initially, when the game starts for the first time. This is my method for that
func addObsticles1ToPool() {
for i in 0...300 {
let element = SKSpriteNode(imageNamed: "obsticle1")
element.hidden = true
obsticle1Pool.append(element)
}
}
Then, my code reads the plist file, and for each of the block calls the following:
func block(x: CGFloat, y: CGFloat, movingUp: Bool, movingSide: Bool, spin: Bool, type: Int16) {
var block: SKSpriteNode!
for obs in obsticle1Pool {
if obs.hidden {
block = obs
break
}
}
block.hidden = false
// Further down the properties of the block are set, such as actions it should perform depending on the input values, also its physics body is set.
I also have methods handling the fact that new elements should be added to the pool as game the proceeds and all that works just fine. The lag time dropped to around 3.5 - 4 secs, but that is still not good enough obviously. I would like to have a game with no lag. However, I am not sure if there is another, more efficient way, to do what I am trying to do than using the sprites pool.
Does anyone know how to reduce this lag time?

I have had the same problem! The issue is in this line...
let element = SKSpriteNode(imageNamed: "obsticle1")
SpriteKit isn't smart enough to know that a texture was already created with that image. So what it is doing is creating that texture over and over again and that is expensive.
Instead create a texture outside of the loop first and then create the sprite node with a texture. Something like this...
let elementTexture = SKTexture(imageNamed: "objstical1")
for i in 0...300 {
let element = SKSpriteNode(texture: elementTexture)
element.hidden = true
obsticle1Pool.append(element)
}
Not only will this be a ton faster it will decrease your apps memory a ton...assuming it was the same issue I was having. Hopefully that helps.

Related

SceneKit RunAction command take too much memory space

I noticed that when application using SceneKit runs the memory usage increase
steadily. After some search I pin pointed it to aNode.runAction.
Every time runAction is called it gets a bit of memory space and never release it.
Because runAction is used often in the application it is obvious that it is going to crash it.
Is there something to do to avoid that problem?
I am using this kind of functions to move some nodes.
I tried moving one node but nothing changed it takes less memory that is all.
func moveMyNodes(x:CGFloat, y:CGFloat, z: CGFloat, speed: CGFloat) {
for k in 0..myNodes.count {
let action = SCNAction.moveBy(x:x, y:y, z:z, duration: speed)
myNodes[k].runAction(action)
}
}
I found the reason.
In loop some of the nodes did not have geometry because they should not be seen.
When .runAction executed for a node without geometry it takes a bit of memory and they add up.
I've just get rid of nodes without geometry.

App Crashes: Memory issue?

I was experimenting a little with Sprite Kit, and I noticed that when I let my app run for a while it crashes.
What I'm trying to do, is to draw a line that follows my player movement, I did this like so:
var ref: CGMutablePathRef = CGPathCreateMutable()
var shapeLine: SKShapeNode!
override func update (){
shapeLine.removeFromParent()
CGPathAddLineToPoint(ref, nil, player.position.x, player.position.y)
shapeLine = SKShapeNode(path: ref)
shapeLine.lineWidth = 3
shapeLine.path = ref
addChild(shapeLine)
}
And this works pretty fine, I remove the node everytime, updating the path, and creating another node with the new path.
The point is that after some seconds (around 25) it crashes.
The nodes are constant since I add and remove everytime one. The framerate starts to decrease right before crashing and goes from 30fps to 20fps.
What I find strange is the use of memory, it increases a lot, getting even to use around 800mb before crashing.
Is there something I'm forgetting, or is it just that the Path increases too much to be handled?
Unfortunately SKShapeNode is bugged, there are many bugs around this library component, most of these are reported here.
You can convert a SKShapeNode to a texture (textureFromNode) than use it with SKSpriteNode.

iOS app in SpriteKit using Swift lags after 6 minutes, any ideas why?

I have been working on an app in SpriteKit that displays a new image after every gesture from the user. During each session, the images are inevitably removedFromParent() but the user calls them frequently.
After using my app for about 6 minutes though, I noticed that it starts to get...choppy. The gestures I have set up use swiping but for some reason when I tap the screen a lot the CPU usage shoots up to 104%?!?
After every swipe, I call this code:
var chooseImage:String = imageCollection[Int(arc4random() % 16)]
var swiperImage = SKSpriteNode(imageNamed: chooseImage)
swiperImage.position = nodePosition
swiperImage.xScale = SH * 0.002
swiperImage.yScale = SH * 0.002
addChild(swiperImage)
This is part of the code that gets called after every swipe I make. Could this be a memory leak? I am at a total loss.
This is is the CPU usage result:
Instead of storing an array of Strings you should try storing the SKTextures for those images. These textures would be created when the object you referenced code from is instantiated. Then you can use those textures to instantiate the nodes later, reducing the amount of unnecessary image loading each time a node is created. It would look something like:
let imageTextures = imageCollection.map { SKTexture(imageNamed: $0) }
...
let swiperImage = SKSpriteNode(texture: imageTextures[Int(arc4random_uniform(imageTextures.count))]
Additionally, you can use arc4random_uniform() to generate a "uniformly distributed random number less than upper_bound" instead of using the mod.
This solution also has the benefit of letting you have multiple nodes use the same texture without having to reload it for each node.
Why don't you clear the child of your parents before you add a new one.

Why are didBeginContact called multiple times?

In an iOS game that uses Sprite Kit along with the contact detection in Sprite Kit's build-in physics engine, I decrease the Hero's number lives by one each time he gets in contact with an enemy. This is done from the didBeginContact method.
However, it seems like that method is not just called once, when the contact begins, but called continuously as long as the Hero and the enemy overlaps: when I set a breakpoint in that method, I can see, that it is the exact same physics body instances that exist as contact.bodyA and contact.bodyB. The result is, that the Hero will lose multiple lives, even though he only passes one single enemy.
If the Hero meets the same enemy again later, he should get one more live subtracted, and therefore I cannot just maintain a seenEnemies hash set to deal with the problem above.
The question is now: how would you make sure that only one live is subtracted for each Hero/enemy contact?
The reason why the didBeginContact is being fired multiple times is because you have multiple contact points happening on concave shapes.
If you look at the picture below, you will see I have 2 sprites, a black star and a red rectangle. When the black star hits the red rectangle, it hits it on multiple points, circled in blue. Sprite Kit will then do a call for each line intersection, so that the developer can use the contactPoint variable for each of these contacts.
I had the same problem (score increasing multiple times for a single enemy destroyed and multiple life points being lost for a single instance of damage.) A user on the Apple forums thinks that it's a bug in [SKPhysicsBody bodyWithTexture:size:] but I don't believe that's the case, because it was happening with other constructors too.
First off, the categoryBitMask and contactTestBitMask are very important, obviously. Take a look at Apple's SpriteKit Physics Collisions sample code:
// Contacts are often a double dispatch problem; the effect you want is based on the type of both bodies in the contact. This sample this in a brute force way, by checking the types of each. A more complicated example might use methods on objects to perform the type checking.
// The contacts can appear in either order, and so normally you'd need to check
each against the other. In this example, the category types are well ordered, so
the code swaps the two bodies if they are out of order. This allows the code
to only test collisions once.
What I did to solve it was setting a flag after handling each condition. In my case, I was testing whether bodyA.node.parent was nil in didBeginContact, because I called removeFromParent() on the missile/enemy nodes to destroy them.
I think you should expect the event to fire multiple times and your code in there has to make sure it's processed only once.
I figured out easy solution:
Just change either body's categoryBitMask value to 0 or non-used value right after it detected contact.
For example:
if (firstBody.categoryBitMask == padCategory && secondBody.categoryBitMask == colorBallCategory) {
secondBody.categoryBitMask = 0;
// DO OTHER THING HERE
}
I came across the same issue. In my case the didBeginContact() was called many times (I counted up to 5 times) for one contact of a bullet with the enemy. As the bullet is a simple circle format, I agree with #SFX that it cannot be a bug just in Texture-Bodies. The tests have shown that there was no call to update() between the didBeginContact() calls. So the solution is simple (Swift):
var updatesCalled = 0
...
internal update() {
updatesCalled ++
}
...
internal func didBeginContact(contact: SKPhysicsContact) {
NSLog("didBeginContact: (\(contact.contactPoint.x), \(contact.contactPoint.y)), \(updatesCalled)")
if(updatesCalled == 0) {return} // No real change since last call
updatesCalled = 0
... your code here ...
}
I tried didEndContact() but that was not called at all. I didn't investigate further into this.
BTW: I just switched from Android, and I'm impressed by the easiness and stability of this System :-)
Here is an option that makes the player invulnerable after being hit for a set time:
A. Create a variable that makes the player invulnerable to losing a life after being hit for a few seconds.
Create a global Boolean variable called isInvuln (set to FALSE) and an NSTimeInterval called invulnTime.
In the method that handles the player and enemy making contact, check to see if isInvuln is False before taking a life. (if isInvuln is true ... do nothing)
If isInvuln is false, take a life then set isInvuln to true.
if(self.isInvuln == FALSE){
self.player.lives-=1;
self.isInvuln = True;}
Add to your updateWithCurrentTime:
if(self.isInvuln==True){
self.invulnTime += timeSinceLast;}
if (self.invulnTime > 3) {
self.isInvuln = FALSE:}
self.invulnTime= 0;
This will make it so that when an enemy and player collide, the player loses a life and becomes invulnerable 3 seconds. After that 3 seconds, the player can take damage again. If the enemy contacts the player within the 3 invulnerable seconds, the contact method does nothing. Hope this helps spark ideas to tackle your problem.
In my experience, didEndContact & didBeginContact are both called multiple times while the objects overlap. This is also happening in SceneKit using iOS 9, so I have to assume it's an intended behavior.

SpriteKit loses textures

I'm making multilevel game based on SpriteKit.
Everything works well except one thing: when user plays long time, changes many levels, etc... then SpriteKit starts losing textures.
I mean there is no big red cross like when image load fails but just empty space like nothing is there.
Hours of debugging and googling did not produce any results.
How can I deal with that bug?
I think I might be having a related issue, except the loss of textures occurs when I am rapidly running actions on a SKSpriteNode. In my code, I run an action each time I get a touch and when the touches are rapid and the animations are firing quickly, the base texture of the SKSpriteNode seemingly disappears. No memory warnings, not a peep from the console; the SKSpriteNode's texture is magically set to nil.
I get the impression from your question that this isn't your exact cause, but you are having the same symptoms. Unfortunately I don't know what is causing it. What I've done to work around the issue has been to constantly check if the texture on my SKSprite node has been set to nil immediately after I run an SKAction and then re-assign it if needed.
So, an abridged version (in Swift) of what I'm doing looks like this :
func doAnimation( ) {
_character.runAction(someSKAction, withKey: "animation")
//Whoops!, we lost our base texture again!
if _character.texture == nil {
let atlas = SKTextureAtlas(named: "someAtlasName")
let texture = atlas.textureNamed("idleFrame" )
_character.texture = texture
}
}
This is not really solution so much as a workaround, but it might be adaptable to your situation until you (or someone else on SO) figures it out. I won't argue that it's not ugly.
BTW, you are not alone with the disappearing texture issue; I wrote a similar response to a similar question here.

Resources