Search code examples
swiftsprite-kitskspritenodeskscene

SKSpriteNode Not Showing


so i have this code:

func addSprites() {
    for row in 0..<GameScene.rows {
      for col in 0..<GameScene.cols {
        let node = grid[row][col]
        if node != nil {
          addChild(node!)
          node?.position = CGPoint(x: (view!.frame.width/CGFloat(GameScene.cols))*CGFloat(col), y: (view!.frame.height/CGFloat(GameScene.rows))*CGFloat(row))
          print(node?.position)
        }
      }
    }
  }

and the grid array has SKSpriteNodes and when i print it this shows

enter image description here

which is fine since i have SKSpriteNode's in it.

The addSprites() outputs

Optional((0.0, 207.0))
Optional((59.733333587646484, 207.0))
Optional((119.46666717529297, 207.0))
Optional((179.1999969482422, 207.0))
Optional((238.93333435058594, 207.0))
Optional((298.6666564941406, 207.0))
Optional((358.3999938964844, 207.0))
Optional((418.1333312988281, 207.0))
Optional((477.8666687011719, 207.0))
Optional((537.5999755859375, 207.0))
Optional((597.3333129882812, 207.0))
Optional((657.066650390625, 207.0))
Optional((716.7999877929688, 207.0))
Optional((776.5333251953125, 207.0))
Optional((836.2666625976562, 207.0))

which is corresponding to the node's location.However as seen here no sprites show:

enter image description here

when I print the touch location in the touchesBegan method and saw the points, even though the points were exactly the same, the sprites don't show. How can I fix this? Also i tried scene.scaleMode = .resizeFill in the GameViewController, however that didn't fix it.


Solution

  • I got it, however I'm not sure why it works, but I changed the anchor point to (0,0) and all of the sprites show!!