The image I set to the background in SpriteKit is not displayed.

Asked 2 years ago, Updated 2 years ago, 79 views

Thank you for your continuous support.
I would like to display the background of SKScene, but why?It will remain black.
The code is the same as the 2D game programming that starts with SpriteKit from Sho Young, but
Where should I go?
If you understand, could you tell me?

Below is the code.

GameScene.swift

import SpriteKit

classGameScene:SKScene{

    override func DidMoveToView (view:SKView) {
        letbackGround=SKSpriteNode(imageNamed: "haikei.png")
        backGround.position = CGPoint (x:self.size.width*0.5, y:self.size.height*0.5)
        backGround.size = self.size
        self.addChild (backGround)
    }
}

GameViewController.swift

import UIKit import SpriteKit

classGameViewController:UIViewController {

    override func viewDidLoad(){
        super.viewDidLoad()

        letscene=GameScene()
        let view=self.view as!SKView
        view.showsFPS=true
        view.showsNodeCount=true
        scene.size=view.frame.size

        view.presentScene(scene)
    }
}

swift spritekit

2022-09-30 21:14

1 Answers

I finally figured out the cause.It seems that the png image was too large to display.
When I reduced the size, I was able to see it clearly.

This post was posted as a community wiki based on @woodpecker's comments.


2022-09-30 21:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.