1

I was creating a Swift Playground using SpriteKit but was having multiple issues. Below is my code:

// Setup

import SpriteKit

import Foundation

import UIKit

import PlaygroundSupport

public class GameScene: SKScene, SKPhysicsContactDelegate {

    var currentScene: SKScene? = nil

    var sceneFrame: CGRect? = nil

    override public func didMove(to view: SKView) {

        physicsWorld.contactDelegate = self

        //Setup Variables Here

    }

override init() {

    sceneFrame = CGRect(x: 0, y: 0, width: 640, height: 320)

    currentScene = SKScene(size: sceneFrame!.size)

    scene?.backgroundColor =
        UIColor(red:0.60, green:0.88, blue:1.00, alpha:1.0)

}

required public init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

var timer: Timer?

trashCreator = Timer.scheduledTimer(withTimeInterval: 4, repeats: true) { (timer) in
        createNode()
    }

func createNode() {

    let node = SKSpriteNode(imageNamed: "node")

    scene!.addChild(trash)

}

//Add Everything to the View

currentScene.addChild(node1)

currentScene.addChild(node2)

//scene.addChild(floor)

let view = SKView(frame: screenFrame)

view.presentScene(scene)

PlaygroundPage.current.liveView = view

}

Keep in mind that I have omitted a lot of other code that sets up the individual SKSpriteNodes. The errors that I am getting are as follows:

Expected Declaration on both the currentScene.addChild(node1) and on the view.presentScene(scene)

Use of unresolved identifier screenFrame on the fourth to last line.

I would appreciate it if you guys could help me solve this as I need to get this project done soon.

kingkps
  • 156
  • 1
  • 13
  • Your last problem looks reasonably obvious: screenFrame does not exist, you probably meant sceneFrame. I cannot see why the first addChild fails when the second does not. Do these other errors still exist with your omitted code? If not, then please put it back or read and follow [How to create a MCVE](https://stackoverflow.com/help/mcve) – Ali Beadle Mar 18 '18 at 07:16
  • The second addChild also fails and yes, these errors exist with my ommited code. – kingkps Mar 18 '18 at 18:33
  • OK, that makes more sense. Both currentScene and view are optionals, so you need to unwrap,them, e.g.: currentScene?.addChild... and view?.presentScene... But there are other problems with your code, e.g. You are subclassing SKScene but then have other scenes called scene and currentScene within it - I doubt that is what you intend. – Ali Beadle Mar 18 '18 at 21:13
  • I ended up fixing the issue by getting rid of the class and just having my code be. My issue now is, how do I detect Collisions and Touches without a class? Can I do that? If not, how do I correctly integrate this code into a class; like what goes where? – kingkps Mar 18 '18 at 21:36
  • I am not aware of a mechanism to allow that kind of thing in a Playground, but I am no expert. I rarely use Playgrounds. I would only point out that Playgrounds are just that - a place to play with ideas, not a basis for a full app. – Ali Beadle Mar 18 '18 at 21:41
  • This is actually a playground for the WWDC 2018 Scholarships. If this was an app, I would’ve been done with everything long ago... – kingkps Mar 18 '18 at 21:42

0 Answers0