0

So I'm trying to create a Playground that functions as a 'presentation', meaning that I have multiple slides and it's interactive-ish. I'm new to Swift, so would really use some help.

This is my main code

import UIKit
import SpriteKit
import PlaygroundSupport

let frame = CGRect(x: 0, y: 0, width: 640, height: 480)

let view = SKView(frame: frame)
view.showsDrawCount = true
view.showsNodeCount = true
view.showsFPS = true

let scene = MainScene(size: CGSize(width: 640, height: 480))
view.presentScene(scene) //error here, explained below
PlaygroundPage.current.liveView = view

This is my MainScene.swift code -

import UIKit
import SpriteKit
import PlaygroundSupport

public class Slide: SKView {

    public var title: SKLabelNode!
    public var info: UITextView!

    public func setter() {

        self.title = SKLabelNode(fontNamed: "Chalkduster")
        self.title.verticalAlignmentMode = .center
        self.title.color = SKColor.white
        self.title.position = CGPoint(x: frame.midX, y: 440)

        let framer = CGRect(x: 40, y: 60, width: 560, height: 360)

        self.info = UITextView(frame: framer)
        self.info.font = UIFont(name: "Chalkduster", size: 20)
        self.info.textAlignment = .center
        self.info.textColor = UIColor.white
        self.info.backgroundColor = UIColor.black
    }


}

public class MainScene: SKScene {

    public var button1 = SKSpriteNode(imageNamed: "Next")
    public var button2 = SKSpriteNode(imageNamed: "Previous")

    public var scene1: Slide?

    public override func didMove(to view: SKView) {

        backgroundColor = SKColor.black
        scene1?.setter()
        setScene1()
    }

    public func setScene1() {

        scene1?.title = SKLabelNode(fontNamed: "Chalkduster")
        scene1?.title.text = "Title."
        scene1?.title.position = CGPoint(x: frame.midX, y: frame.midY)
        scene1?.info.text = "Text."

        addChild(scene1?.title)
        addButtons()
    }

    public func addButtons() {

        self.button1.position = CGPoint(x: 600, y: 40)
        self.button2.position = CGPoint(x: 40, y: 40)
        addChild(self.button1)
        addChild(self.button2)
    }

}

Basically, I was tying to setup multiple slides through functions that would change the previous slide's title/info. However, I thought it would be much better to have an SKView class defined for my slides in order to use SKTransition.

However, with my current code, I'm running into an execution interrupted error. Also, when I change stuff around, the error kind of followed me into either Expected Declaration or no Class Initializers for MainScene.swift.

Really, I just want to fix up my declaration of the class Slide and then use that class to make multiple slides.

Austin
  • 21
  • 3
  • You are never calling `scene1 = Slide()`, so you are never making a new instance of `Slide` – dmorrow Mar 31 '17 at 15:05
  • public var scene1: Slide? Shouldn't this suffice? – Austin Mar 31 '17 at 15:07
  • I just dropped this code into a playground. `addChild(title)` won't work – Ashley Mills Mar 31 '17 at 15:11
  • That sets a variable of type `Slide`. Please review the Apple docs - https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html. You need to create an instance and set that variable to that instance. – dmorrow Mar 31 '17 at 15:11
  • 1
    I also don't think you should use `SpriteKit`. Get started with `UIKit` and `UIView` first. That's the basics. – dmorrow Mar 31 '17 at 15:12
  • @AshleyMills Yeah, I fixed that. An error that crept in while copying. Thanks – Austin Mar 31 '17 at 15:13
  • @dmorrow Setting it to Slide() worked! Thanks! – Austin Mar 31 '17 at 15:14
  • `addChild(scene1?.title)` won't work either. You need something like `if let title = scene1?.title { addChild(title)}` – Ashley Mills Mar 31 '17 at 15:16
  • @AshleyMills Yeah basically.. as dmorrow pointed out, I had to create an instance and then set the variable. After I called the setter() function, I could get rid of all the "?"'s in my code. Thanks for your help! – Austin Mar 31 '17 at 15:19

1 Answers1

1

Here's an example without SpriteKit. You should really review some basic Swift and UIKit lessons to get a good foundation on the language

import UIKit
import PlaygroundSupport

public class Slide: UIView {

    public var title: UILabel
    public var info: UITextView

    required public override init(frame:CGRect) {
        self.title = UILabel()
        self.title.font = UIFont(name: "Chalkduster", size: 20)
        self.title.textColor = UIColor.white
        self.title.backgroundColor = UIColor.red
        self.title.center = CGPoint(x: frame.midX, y: 440)

        self.info = UITextView()
        self.info.font = UIFont(name: "Chalkduster", size: 20)
        self.info.textAlignment = .center
        self.info.textColor = UIColor.white
        self.info.backgroundColor = UIColor.blue

        super.init(frame: frame)
        addSubview(title)
        addSubview(info)

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

public class MainScene: UIView {
    public var scene1: Slide

    required public override init(frame: CGRect) {
        scene1 = Slide()
        super.init(frame: frame)
        scene1.title.text = "Title."
        scene1.title.sizeToFit()
        scene1.title.center = CGPoint(x: frame.midX, y: frame.midY - 20)
        scene1.info.text = "Text."
        scene1.info.sizeToFit()
        scene1.info.center = CGPoint(x: frame.midX, y: frame.midY + scene1.title.frame.height + 20)
        addSubview(scene1)
    }
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

let frame = CGRect(x: 0, y: 0, width: 640, height: 480)

let view = UIView(frame: frame)

let scene = MainScene(frame: frame)
view.addSubview(scene)
PlaygroundPage.current.liveView = view
dmorrow
  • 5,152
  • 5
  • 20
  • 31