Search code examples
swiftsprite-kitscreenskspritenodemetal

SpriteKit: Add SKSpriteView To The View


I have an image for the background, which I am trying to assign to the screen

My code:

let image = SKSpriteNode(imageNamed: "background")
func addBackground(sizeNode: CGFloat){
    image.size = CGSizeMake(sizeNode, sizeNode)
    image.position.x = xPos
    image.position.y = yPos
    GameScene().addChild(image)
}

The Programm compiles with no error messages, but on my screen is no visible node (node counter = 1 should be 41) and I got following line in the console:

'<'CAMetalLayer: 0x13cd07c10>: calling -display has no effect.

Hopefully you can help me out!


Solution

  • Your last line creates a new instance of a GameScene and adds the image to it. This instance is not assigned to a variable and never added to the screen.

     GameScene().addChild(image)
    

    Check if you have already created a screen and presented it in an skview. Add your background to this scene