1

I'm building a platformer, and I'm trying too learn about being efficient with memory and cpu cycles. Is there an efficient way to handle sprites that are off screen?

I guess the idea is, only handle the sprites on screen or close to it, all other sprites can be unconsidered to free up the handling that those sprites would take.

I have an array of all my sprites, so my first thought was look through them all and see if the camera, contains each one... If so add them to the screen as a child. If camera doesn't contain the sprite, then remove it from it's parent...

The issue I'm having, is that my character disappears even on screen, and I can't move him... Am I going about this wrong?

Thanks for any advice!!!

Update: "I'm not at my computer now, so syntax may be a little off"

for sprite in allSprites {
    if camera.contains(sprite) {
        sprite.removefromparent()
        self.addChild(sprite)
    }
    else {
        sprite.removefromparent()
    }
}

When I get home I can update it to accurate code, and print the view and scene size.

Discoveringmypath
  • 1,049
  • 9
  • 23
  • Take a look at this: https://developer.apple.com/documentation/spritekit/skview/1519683-shouldcullnonvisiblenodes About the issue... Show the code which removes nodes, and print your view and scene size... – Whirlwind Jun 19 '17 at 08:46
  • I think Apple had done that. Enable your `SKView.showsNodeCount` and move your camera, the right-bottom nodes count is changed, which means current render nodes is only visible nodes. – sakiM Jun 19 '17 at 08:46
  • @Whirlwind thanks for the reference. I've seen this before which makes me believe that I don't need to handle this. Can I optimize this by doing it manually and setting the value to false, or does apple do a good job with this. I will update the question with my code. – Discoveringmypath Jun 19 '17 at 13:56
  • @sakiM I'm not sure that my nodecount changes, I would have to check later. the draw count does change, depending on what elements are on screen or not. – Discoveringmypath Jun 19 '17 at 13:57
  • @Discoveringmypath You should try both cases, and see what works best for you. From what I have seen, keeping this property to true works great. Are you experiencing performance issues, or we are having here a premature optimization? – Whirlwind Jun 19 '17 at 14:03
  • It might be premature optimization lol. (love the wording). I'm not experiencing lag yet, but I haven't put in everything that I want as well and I'm going to be working with an open world kind of 2d game. I'm guessing when it comes to rendering graphics, I should let Apple do its thing on its own. But the physics calculations, actions and other things like that, I'm guessing there is a way to be more efficient with that. – Discoveringmypath Jun 19 '17 at 23:42
  • You need to be careful because your optimizations may be more costly than just leaving the sprites on screen. especially doing it in a for loop like that. I would recommend building your game first, then worry about having to optimize. When you do need to optimize, I would recommend checking if the node frame intersects with the camera frame – Knight0fDragon Jun 22 '17 at 19:18
  • @Knight0fDragon The code above wouldn't work and currently I'm not implementing it. My game currently doesn't experience lag except in random moments once in a blue moon it will lag for a second or two. I don't like this. I haven't implemented everything yet also. My cpu usage is around 45-50% and the energy efficient reading is "very high". So that concerns me. But I agree I'm going to hold off and focus on just putting together the game until I experience lag more often. Either way I'm not satisfied with the lag I'm getting so far even if it is minimal. – Discoveringmypath Jun 23 '17 at 14:53
  • 1
    Learn how to use the test project to find your lag issues – Knight0fDragon Jun 23 '17 at 14:54
  • test project? Do you have references to how to accomplish this. – Discoveringmypath Jun 23 '17 at 14:55
  • No, but look at your solution, you should see a test and uitest project, play with then and find some tutorials – Knight0fDragon Jun 23 '17 at 19:21
  • @Knight0fDragon I'll look into it, u said solution, I'm assuming you mean project? And okay, I'll have to research how to do uitests and tests... – Discoveringmypath Jun 23 '17 at 20:14
  • yeah sorry, talking Visual Studios here lol – Knight0fDragon Jun 24 '17 at 00:52

0 Answers0