Cannot convert value of type 'SKScene.Type' to expected argument type 'SKScene'

I am getting this, "Cannot convert value of type 'SKScene.Type' to expected argument type 'SKScene'"


this is my code for the problem

self.scene?.addChild(itemController.spawnItems(scene: SKScene));



This is my whole code.

/
/
/
/
/
/
/ 
import SpriteKit
class GameplaySceneClass: SKScene {
   
    private var Player: Player?;
   
    private var center = CGFloat();
   
    private var canMove = false
    private var moveLeft = false
   
    private var itemController = ItemController();
   
   
    override func didMove(to view: SKView) {
        initializeGame();
       
    }
   
   
    override func update(_ currentTime: TimeInterval) {
        managePlayer();
    }
   
   
   
   
   
   
   
   
    private func initializeGame() {
        Player = childNode(withName: "Player") as? Player!;
       
        center = self.frame.size.width / self.frame.size.height;
       
        Timer.scheduledTimer(timeInterval: TimeInterval(itemController.randomBetweenNumbers(firstNum: 1, secondNum: 2)), target: (Any).self, selector: #selector(GameplaySceneClass.spawnItems), userInfo: nil, repeats: true)
    }
   
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       
        for touch in touches {
            let location = touch.location(in: self);
           
            if location.x > center {
                moveLeft = false;
            } else {
                moveLeft = true;
            }
        }
        canMove = true;
    }
   
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        canMove = false;
    }
   
   
    private func managePlayer() {
        if canMove {
            Player?.move(left: moveLeft);
        }
    }
   
    @objc func spawnItems() {
        self.scene?.addChild(itemController.spawnItems(scene: SKScene));
    }
   
} /

The cause of the error is that you're trying to invoke a "spawnItems" method in the ItemController that has a parameter with keyword "scene", which is apparently supposed to be a SKScene object. By writing "SKScene" literally in that method invocation:


     … spawnItems(scene: SKScene)


you're actually passing a SKScene type object, which isn't acceptable to the method. Presumably you're trying to pass an actual scene, but it's not clear which one. (Maybe you can clarify, if this doesn't resolve your question.)

In the video its

self.scene?.andChild(itemController.spawnItems());

.

but that gives me this error. " Value of type 'SKScene' has no member 'andChild' "


I think it would be better to fix this one.

In what video? IAC, you said "addChild" all the other times, which makes more sense than "andChild".

Sorry just a typign error. This is what I meant to put.

self.scene?.addChild(itemController.spawnItems());

Now I get these.

Editor placeholder in source file

Missing argument for parameter 'scene' in call Insert 'scene: <#SKScene#>'

If I add the SKScene and hit fix on the error I dont know what to put for SKScene.


The video is https://www.youtube.com/watch?v=dFD54_1YDJk&t=1361s

You write andChild instead of addChild, hence the problem.


You have many threads opened on the same topic. Please close those which are over.

All are for different topics. If you read the other post I said that I used add child it was just a typing error when I was puting it into the forums.


I **** at spritekit and probobly wont make a game ever again. I only make single and multi tabbed applications and never need help with those.

Sorry, but I do not learn your posts by heart ! It is better to take care to post exactly what you typed. Are the other posts still pending or solved ?

the other posts are solved. I do not know how to close them.

To close, you just click "Correct answer" on the correct one. May even be yours, but it should let others find easlily the solution when they read the post.

There is something not clear in your code intent :


@objc func spawnItems() {
        self.scene?.addChild(itemController.spawnItems(scene: SKScene));
    }



1. How is the class ItemController defined ?


2. As you call itemController.spawnItems(scene: SKScene) on an instance of ItemController, the func should be defined in ItemController. Is that the case ?


3. You have defined 2 methods: spawnItems() and spawnItems(scene: SKScene)

Is that intentional ?


4. Anyway, when you call

itemController.spawnItems(:),

the parameter must be an instance of SKScene ; but you must not give the type, just call:

itemController.spawnItems(scene: aScene)

where aScene is an instance of SKScene

Cannot convert value of type 'SKScene.Type' to expected argument type 'SKScene'
 
 
Q