无法为Spritekit swift创建自定义对话框

时间:2015-08-29 16:20:03

标签: ios swift sprite-kit

我目前在为spritekit游戏制作弹出对话框时遇到问题。我想要做的是显示按下游戏开始按钮时显示的自定义对话框。我能够创建自定义对话框,但问题是如何为这个自定义对话框创建一个按钮,扩展UIView以转换到另一个场景?当选择开始按钮时,下面的代码将使标题场景中的对话框显示

let dialog = CustomDialog(scene: self, frame:CGRectMake(0, 0, self.view!.bounds.maxX - 50, 300))
    self.view!.addSubview(dialog)

但是如何在CustomDialog类中实现这个脚本?

 let scene = GameStartedScene(size: self.size)
            scene.scaleMode = SKSceneScaleMode.AspectFill
            skview.presentScene(scene)

下面的代码是CustomDialog类,有没有办法让这个类转换到其他场景?由于我是swift和spritekit的新手,如果我能有一些样本或提示,我将感到很荣幸。

import UIKit
import SpriteKit

class CustomDialog : UIView{

    var backGroundView : UIView!
    var scene : SKScene!

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    init(scene : SKScene,frame : CGRect){
        super.init(frame: scene.view!.bounds)


        self.scene = scene


        self.scene.view!.paused = true


        self.scene.userInteractionEnabled = false

        self.layer.zPosition = 10


        self.backGroundView = UIView(frame: scene.view!.bounds)
        self.backGroundView.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.3)
        self.backGroundView.layer.position = scene.view!.center
        self.addSubview(backGroundView)


        let board = UIView(frame: frame)
        board.backgroundColor = UIColor.whiteColor()
        board.layer.position = backGroundView.center
        board.layer.masksToBounds = true
        board.layer.cornerRadius = 20.0
        board.layer.borderColor = UIColor.blackColor().CGColor
        self.addSubview(board)


        let textView = UILabel(frame: CGRectMake(0, 0, 200,50))
        textView.text = "hogehoge"
        textView.textAlignment = NSTextAlignment.Center
        textView.layer.position = backGroundView.center
        textView.backgroundColor = UIColor.clearColor()
        textView.textColor = UIColor.blackColor()
        self.addSubview(textView)


        let myWindowExitButton = UIButton.buttonWithType(UIButtonType.ContactAdd) as! UIButton
        myWindowExitButton.tintColor = UIColor.blackColor()
        myWindowExitButton.layer.position = CGPointMake(board.bounds.maxX - myWindowExitButton.bounds.midX - 5, myWindowExitButton.bounds.midY + 5)

                   myWindowExitButton.transform = CGAffineTransformMakeRotation(CGFloat((45.0 * M_PI) / 180.0))

        myWindowExitButton.addTarget(self, action: "onExitButton:", forControlEvents: UIControlEvents.TouchUpInside)
        board.addSubview(myWindowExitButton)

    }

    func onExitButton(sender : UIButton){

        self.scene.view!.paused = false
        self.scene.userInteractionEnabled = true
        self.removeFromSuperview()

    }

}

0 个答案:

没有答案