0

I have an animation in the menu of my app. I initialise it in the view controller, store in an array and then access and run the animation like this:

let animationFrames = SKAction.animateWithTextures(animationTextures.animationFrames[0], timePerFrame: 0.05)
let repeatAnimation = SKAction.repeatActionForever(animationFrames)

animation = SKSpriteNode(texture: animationTextures.firstFrame[0])
animation.size = CGSizeMake(self.frame.size.width, 150)
animation.position = CGPoint(x: CGRectGetMidX(self.frame), y: self.frame.size.height - 75)
animation.runAction(repeatAnimation)

The problem is that it allocates way too much memory than it's supposed to. It's something like 4-5MB animation, but from what I can see when I run the app it allocates memory every time the animation starts over and it ends up taking about 50MB after 5-6 iterations and then it doesn't allocate any additional memory.

It's ok on iPhone 5 and above because there's enough memory to handle the app, but since I don't have just this one animation but a couple it ends up allocating 200MB and causes my app to crash on iPhone 4 and 4s.

Mario Plantosar
  • 804
  • 9
  • 24
  • My guess is you have way too many textures to animate what you want and they are big too. .05 time per frame is rather quick plus it looks like they are the entire width of the screen. As far as the app keeps growing in size that shouldn't be a thing. I would make sure you are only calling that code once. If you kept calling the above code in an update loop that could cause the app to grow. If you can't figure it out I would recommend moving that over to a dummy app and make sure that is actually the offending code. – Skyler Lauren Apr 03 '15 at 12:28
  • @MarioPlantosar How many images you use for one animation ? What dimension those images have? Do you use texture atlases ? – Whirlwind Apr 03 '15 at 12:28
  • 1
    It sounds like you are running your animation from a number of places in your code and not keeping a pointer to the first instance (an array for example). I say that because you claim that a 5MB animation runs a couple of times and then tops out around 50MB. – sangony Apr 03 '15 at 12:48
  • The animations get called on viewDidLoad so it shouldn't get called more than once, and there are 60 frames in each of the 2 animations. I tried to remove everything else and concluded that it's really the 2 animations I use that take up all this space :/ – Mario Plantosar Apr 04 '15 at 10:07
  • If you isolated your memory issue to just loading and running the animations once, then you need to look at the pics making up the animation. What graphics format are you using? PNG, JPEG, something else? What size is each frame pic? – sangony Apr 04 '15 at 13:22
  • You could save a lot of RAM at runtime using the texture compression described in this SO answer (See SpriteKitFireAnimation example) http://stackoverflow.com/a/38679128/763355 – MoDJ Jul 31 '16 at 19:56

0 Answers0