22

What would be the best way to draw a grid like this by using the SpriteKit 2D game engine? Requirements:

  • Input programatically the number of columns and rows (5x5, 10x3, 3x4 etc.).
  • Draw it programmatically using something like SKSpriteNode or SKShapeNode, since just using images of a square like this doesn't seem very efficient to me.
  • The squares should have a fixed size (let's say each is 40x40).
  • The grid should be vertically and horizontally centred in the view.

I'm planning to use a SKSpriteNode (from an image) as a player moving in different squares in this grid.
So, I'll save in a 2 dimensional array the central point (x,y) of each square and then move from the player's current position to that position. If you have a better suggestion for this too, I'd like to hear it.

I would appreciate a solution in Swift (preferably 2.1), but Objective-C would do too. Planning on using this only on iPhone devices.
My question is close to this one. Any help is appreciated.

4
  • Why not use CGContext (Quartz) to draw the lines? Look at CGPaths. The only care you need to take is make sure that the coordinates co-incide. After all, sprites are sprites...
    – user3821934
    Nov 1, 2015 at 17:27
  • Does the grid need on the hole screen because the you should just divide the lengt of the screen throw the lengt of the grid you want.
    – Cing
    Nov 1, 2015 at 17:46
  • To answer your question, no, the grid does not need to be on the whole screen.
    – Alex
    Nov 2, 2015 at 10:26
  • Hey, Michael L! Thanks for the suggestion. I didn't know how to use those CGPaths in an efficient way, but 0x141E's answer did that perfectly.
    – Alex
    Nov 2, 2015 at 10:28

1 Answer 1

41

I suggest you implement the grid as a texture of an SKSpriteNode because Sprite Kit will renders the grid in a single draw call. Here's a example of how to do that:

class Grid:SKSpriteNode {
    var rows:Int!
    var cols:Int!
    var blockSize:CGFloat!

    convenience init?(blockSize:CGFloat,rows:Int,cols:Int) {
        guard let texture = Grid.gridTexture(blockSize: blockSize,rows: rows, cols:cols) else {
            return nil
        }
        self.init(texture: texture, color:SKColor.clear, size: texture.size())
        self.blockSize = blockSize
        self.rows = rows
        self.cols = cols
    }

    class func gridTexture(blockSize:CGFloat,rows:Int,cols:Int) -> SKTexture? {
        // Add 1 to the height and width to ensure the borders are within the sprite
        let size = CGSize(width: CGFloat(cols)*blockSize+1.0, height: CGFloat(rows)*blockSize+1.0)
        UIGraphicsBeginImageContext(size)

        guard let context = UIGraphicsGetCurrentContext() else {
            return nil
        }
        let bezierPath = UIBezierPath()
        let offset:CGFloat = 0.5
        // Draw vertical lines
        for i in 0...cols {
            let x = CGFloat(i)*blockSize + offset
            bezierPath.move(to: CGPoint(x: x, y: 0))
            bezierPath.addLine(to: CGPoint(x: x, y: size.height))
        }
        // Draw horizontal lines
        for i in 0...rows {
            let y = CGFloat(i)*blockSize + offset
            bezierPath.move(to: CGPoint(x: 0, y: y))
            bezierPath.addLine(to: CGPoint(x: size.width, y: y))
        }
        SKColor.white.setStroke()
        bezierPath.lineWidth = 1.0
        bezierPath.stroke()
        context.addPath(bezierPath.cgPath)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return SKTexture(image: image!)
    }

    func gridPosition(row:Int, col:Int) -> CGPoint {
        let offset = blockSize / 2.0 + 0.5
        let x = CGFloat(col) * blockSize - (blockSize * CGFloat(cols)) / 2.0 + offset
        let y = CGFloat(rows - row - 1) * blockSize - (blockSize * CGFloat(rows)) / 2.0 + offset
        return CGPoint(x:x, y:y)
    }
}

And here's how to create a grid and add a game piece to the grid

class GameScene: SKScene {
    override func didMove(to: SKView) {
        if let grid = Grid(blockSize: 40.0, rows:5, cols:5) {
            grid.position = CGPoint (x:frame.midX, y:frame.midY)
            addChild(grid)

            let gamePiece = SKSpriteNode(imageNamed: "Spaceship")
            gamePiece.setScale(0.0625)
            gamePiece.position = grid.gridPosition(row: 1, col: 0)
            grid.addChild(gamePiece)
        }
    }
}

Update:

To determine which grid square was touched, add this to init

self.isUserInteractionEnabled = true

and this to the Grid class:

override func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let position = touch.location(in:self)
        let node = atPoint(position)
        if node != self {
            let action = SKAction.rotate(by:CGFloat.pi*2, duration: 1)
            node.run(action)
        }
        else {
            let x = size.width / 2 + position.x
            let y = size.height / 2 - position.y
            let row = Int(floor(x / blockSize))
            let col = Int(floor(y / blockSize))
            print("\(row) \(col)")
        }
    }
}
6
  • 1
    Really appreciate the solution, @0x141E! It's exactly what I was looking for. Really cool how you got SpriteKit to render the grid in a single draw. Additional thank you for the gridPosition function, it's better than what I had in mind.
    – Alex
    Nov 2, 2015 at 10:24
  • How would you differentiate every box in the grid? Say I add skspritenodes as children of Grid class, how would I use touches began for every box in the grid?
    – Parth Sane
    Feb 25, 2017 at 15:31
  • Adding touchesBegan to Grid class does not work. You need to add it to the node instance which is added to the grid.I suspect this is because you're adding the box in the grid as a child of the entire grid sprite. So Instead we need to inherit SKSpriteNode in a different class for every box and override touchesBegan over there. That's what worked for me. I figured this out the hard way. Thanks for your answer.
    – Parth Sane
    Feb 27, 2017 at 5:03
  • @Boggartfly see the update to the touchesBegan method.
    – 0x141E
    Feb 27, 2017 at 8:52
  • Would it be better to use a SKTileMapNode for this? Nov 1 at 9:20

Your Answer

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

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