2

I'm a total newcomer to SpriteKit and game development in general, I've been toying with SpriteKit to make a strategy game set in space.

My backend architecture use a grid system to represent the Universe, I have empty cases and cases with systems/planets/etc... My grid is backed by GameplayKit GKGridGraph, I use an algorithm that generate a node with random properties for each node of the grid and I subclassed it to add a custom entity to it, which all the properties of this specific universe case.

To render it, I simply use SKShapeNode and SKPriteNode with various colors, shapes and textures.

I enumerate all nodes (GKGridGraphNode) in my GKGridGraph instance, and for each of those nodes I get generate the corresponding SKNode (my SKNode generations is a component of each GKGridGraphNode entity attached object), and I set them a position, and add them as a child to my main node (let's call it mapNode) which is a simple SKNode. In the end it looks like a grid.

It works well for a 30/30 grid, I have 60 FPS while scrolling my grid (custom implementation, I modify my mapNode positon as the user move his fingers). But as soon as I try a 50/50 or a 100/100 grid, I have literally too many nodes on the screen for the scrolling to works. I know I shouldn't add every of my node on the screen, so I thought about various strategies and I wanted some input on them:

  • Instead of scrolling my mapNode, I could render only the nodes I see on the screen, and then add/remove nodes as the user scroll left/right/up/down. So it's not really scrolling anymore, it simulate it. I can think of it, but not really how I should implement it in practice. Is it the right solution?

  • Maybe I could render all my node as one big node? Is there a way to do that? But then I'll loose functions such as nodeAtPosition, which I use extensively to get the entity (custom object) associated with my nodes.

Edit: Actually, my current code is open source, here is the scene in I'm rendering: https://github.com/Dimillian/LittleOrion/blob/master/Little%20Orion/Little%20Orion/scenes/UniverseScene.swift

Is there any other smart way of doing that?

3
  • 1
    have you confirmed this is slow with an actual device?
    – hamobi
    Nov 28, 2016 at 23:08
  • Yeah, 17 FPS on the simulator, 40 FPS as a static node on device, but as soon as your start moving your finger, it's stall to less than 1 FPS, and you can't effectively scroll it.
    – Dimillian
    Nov 29, 2016 at 10:30
  • 1
    You have to dynamically load and unload data into sprite kit nodes as the content is scrolled. You simply cannot load all the data ahead of time, just change your code to dynamically test square overlap in order to determine which nodes to load and render. This is basically the same as tiling, so you would likely want to look into the new Tile support in iOS 10 and build on that.
    – MoDJ
    Nov 29, 2016 at 22:00

1 Answer 1

2

SKTileMapNode was made just for this in Xcode 8 https://www.raywenderlich.com/137216/whats-new-spritekit-ios-10-look-tile-maps

Alternatively, you would only want to load the nodes that are in and near your current view. You would need an algorithm to do this, and would be a big headache compared to tilemaps.

I suspect the algo would involve checking which nodes are in the view's .frame' and then using 'addChild' on them--concurrently, add a reference to them to an array, which you would check against nextupdate()andremoveFromParent` if they were no longer visible .. It would get hairy trying to optimize this though. The goal would be to load only a few nodes out on each end so that way you have some buffer in moving the camera (less updates).

You could create a math function to pre-determine which nodes are where in relation to the current frame coordinates--so you don't have to iterate through the nodes--but that would require even a lot more work and headache--and it's what people developing on consoles, etc, have to do with high-end games and limited power.

I recommend skimming through a Direct3d/DirectX/OpenGL game development book, just to get an idea of what goes into everything... They aren't hard to find: walk into a bookstore and look for the thickest / heaviest book--that will be the DirectX game development book.

You will see how what we can do with 30 lines in SK requires thousands of lines and vector calculus in C++ and low-level AV frameworks. It will give you an appreciation, understanding, and perspective of game dev, which will help you in your SK journeys :)

3
  • 1
    I have some (very) basic knowledge on how the backend stuff works, I've made it a hobbyto study games dev and game engine, so I knew from start that adding every nodes on screen would be inefficient (unless SK did some magic a la UITableView/UICollectionView reuse), but I understand that SKTilMapNode do just that :D. Actually I have 1 OpenGL books on my desk hehe, but if it's all gibberish, I love to read that :) But yeah, coding the game VS coding the engine is a deal breaker for me, I don't have time to move to full low level code even if I would love to :(
    – Dimillian
    Nov 30, 2016 at 13:57
  • for sure, I only brought it up because you mentioned that you were new to game dev ;) And yes, gibberish indeed!
    – Fluidity
    Nov 30, 2016 at 13:59
  • Yeah, I'm new anyway, new on doing it, and cheap because I took the "easiest' way :D But it's very fun coming from apps development, a very different way of thinking (I'm looking at you update loop).
    – Dimillian
    Nov 30, 2016 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.