Search code examples
iosswiftsprite-kitskspritenode2d-games

Build a grid level for a game with SpriteKit and SWIFT


I’m searching the best way to build the levels for a game :

  • The game will be composed with many levels, each levels will be unique.
  • You’ll play an object (here the star), which is in movement on a grid.
  • The object can move only to the bottom, it cannot go back.
  • A level is composed with many cases, each cases will have a different behavior.
  • My level will have a background, width of the screen, but the height will depend on the level, I suppose many times the height of the screen.
  • The background moves depending on the object movements

For now, I’m working on a little prototype, just to learn.

proto

After reading many tutorials, my idea is to create x squares (diamond-shape) using SKSpriteNode. And to identify the squares with identifiers.

I don’t know if it’s a good solution in terms of performance? They will be a lot of squares in one level. I don’t realize :)

Are we limited with the numbers of node in one scene?


Solution

  • I think it's not necessary. You can also use a single SKSpriteNode background populated by squares CGPath's and you can make a very fast game if you want to increase difficult levels. You can organize your "functional" part of game by working with each CGPath and when you must change a square in this case you can create a square using the CGPath that represent that square, give to it a name based for example to an index to easily understand who it is, using the CGPath involved and use this SKSpriteNode to:

    • cover the square background
    • make an effect like explosion
    • change color of a square instead of general square colors
    • etc..

    P.S.: I've seen you want to create a scrollView, there is a nice Spritekit library in swift called CustomScrollView here that can be useful , it's simply to use:

    scrollView = CustomScrollView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height), scene: self, moveableNode: moveableNode, scrollDirection: .Vertical)
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height * 3) // makes it 3 times the height
    view?.addSubview(scrollView)
    

    Update: about your comment, the answer is yes: when you work with a path you work with a group of elements organized in points (take a look at this post to understand how).

    You can use the native:

    CGPathContainsPoint(<#T##path: CGPath?##CGPath?#>, <#T##m: UnsafePointer<CGAffineTransform>##UnsafePointer<CGAffineTransform>#>, <#T##point: CGPoint##CGPoint#>, <#T##eoFill: Bool##Bool#>)
    

    or use this:

    CGPathGetPathBoundingBox(<#T##path: CGPath?##CGPath?#>)
    

    to use it:

    CGRectIntersectsRect(<#T##rect1: CGRect##CGRect#>, <#T##rect2: CGRect##CGRect#>)