我如何通过在快速操场上按下按钮来调用场景

问题描述 投票:0回答:1

单击该按钮后如何使此按钮打开GameScene1?到目前为止,这是我的代码。

class GameScene1: SKScene{
    override func didMove(to view: SKView){
    }
}
let scene1 = GameScene1(size: CGSize(width: 400, height: 640))
scene1.scaleMode = .aspectFill
scene1.backgroundColor = .blue

let view2 = SKView(frame: CGRect(x:0, y:0, width: scene1.size.width, height: scene1.size.height
view2.presentScene(scene)
PlaygroundPage.current.liveView = view2



class Receiver{
    @objc func buttonClicked(){
    }
}
let view1 = UIView()
let receiver = Receiver()
let button = UIButton(frame: CGRect(x:10, y:10, width:100, height:50))
button.setTitle("start", for: .normal)
button.addTarget(receiver, action: #selector(Receiver.buttonClicked), for: .touchUpInside)
view.addSubview(button)

我不知道该怎么做。

swift swift-playground
1个回答
0
投票

[好,我对SKScene的经验不足,但这就是我要做的。实际上,您在操场上只需要一个SKView,就可以将其作为static let存储在带有游戏变量的文件中。然后,您可以创建不同的场景,例如一个用于菜单,一个用于第一级,第二级等。

以下代码创建一个按钮并为其提供操作。该动作将创建一个称为场景的常量,这是您要移至下一个场景。然后,您只需调用presentScene()和场景,然后调用一个SKTransition,如下图所示,它只是从右边开始的2.5秒滑动动画。

ButtonNode类,它只是一个充当按钮的节点,您可以将其添加到场景中。

class ButtonNode: SKSpriteNode {

    var action: ((ButtonNode) -> Void)?

    var isSelected: Bool = false {
        didSet {
            alpha = isSelected ? 0.8 : 1
        }
    }

    required init(coder: NSCoder) {
        fatalError("NSCoding not supported")
    }

    init(texture: SKTexture, size: CGSize) {
        super.init(texture: texture, color: SKColor.white, size: size)
        isUserInteractionEnabled = true
    }

    override func touchesBegan(with event: NSEvent) {
        action?(self)
    }

    override func mouseDown(with event: NSEvent) {
        action?(self)
    }
}

创建按钮并为其提供移动到下一个场景的动作。这可以在任何SKScene类中完成。

startButton = ButtonNode(texture: SKTexture(imageNamed: "start-button"), size: CGSize(width: 184, height: 72))
        startButton.position = CGPoint(x: 0.0, y: 0.0)
        startButton.action = { (button) in
            if let scene = IntroCutScene(fileNamed: "IntroCutScene") {
                // Set the scale mode to scale to fit the window
                self.scene!.scaleMode = .aspectFill

                // Present the scene
                GameVariables.sceneView.presentScene(scene, transition: SKTransition.moveIn(with: SKTransitionDirection.right, duration: 2.5))
            }
        }
        self.addChild(startButton)
© www.soinside.com 2019 - 2024. All rights reserved.