使用SKTransition继承SKScene类

时间:2017-03-31 20:47:03

标签: swift swift3 sprite-kit uikit swift-playground

我正在制作一个类似“演示文稿”的游乐场,也就是说你可以添加幻灯片和点击按钮来前后移动。但是,我正在尝试使用SKTransition从scene1转到scene2,但转换会滚动我的标题和按钮。可以真的使用一些帮助,谢谢!

主要代码 -

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)
PlaygroundPage.current.liveView = view

MainScene.swift -

import UIKit
import SpriteKit
import PlaygroundSupport

public class Slide: SKScene {

    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: 320, 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

        self.backgroundColor = SKColor.black
    }


}

public class MainScene: SKScene {

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

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

    public var scene1 = Slide()
    public var scene2 = Slide()

    public override func didMove(to view: SKView) {

        backgroundColor = SKColor.black

        scene1.setter()
        scene2.setter()
        setTheStage()

    }

    public func setTheStage() {

        switch sceneNo {
        case 1:
            setScene1()
        default:
            setScene2()
        }

    }

    public func setScene1() {

        scene1.title?.text = "Scene1Title"
        scene1.title?.position = CGPoint(x: frame.midX, y: frame.midY)

        title = scene1.title

        addChild(title)
        addButtons()

    }

    public func setScene2() {

        scene2.title?.text = "Scene2Title"
        scene2.info?.text = "Scene2Text"

        title = scene2.title
        info = scene2.info

        let transition = SKTransition.flipVertical(withDuration: 1.0)
        self.view?.presentScene(scene2, transition: transition)

        addChild(title)
        self.view?.addSubview(info)
        addButtons()

    }

    public func addButtons() {
        self.button1.position = CGPoint(x: 600, y: 40)
        self.button2.position = CGPoint(x: 40, y: 40)

        if sceneNo < 5 {
            addChild(self.button1)
        }

        if sceneNo > 1 {
            addChild(self.button2)
        }
    }

    override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        for touch in touches {

            let location = touch.location(in: self)

            if(button1.contains(location)) {

                self.button1.removeFromParent()
                self.button2.removeFromParent()
                self.title.removeFromParent()

                if sceneNo > 1 {

                    self.info.removeFromSuperview()

                }

                sceneNo += 1
                setTheStage()
            }
            else if(button2.contains(location)) {

                self.button1.removeFromParent()
                self.button2.removeFromParent()
                self.title.removeFromParent()
                self.info.removeFromSuperview()

                sceneNo -= 1
                setTheStage()

            }
        }
    }

}

0 个答案:

没有答案