Ui按钮没有添加到SkScene

时间:2017-02-22 05:31:15

标签: swift button uibutton skscene

我正在尝试为我的游戏创建菜单但是我的Ui按钮没有添加到场景中,有人能看到我在这里做错了吗?我知道问题是显而易见的,我只是想念它大声笑。我把它们添加到场景错了吗?或者有什么我忘了定义

import Foundation
import SpriteKit

var nextLevelButton = UIButton ()

var previousLevelButton = UIButton ()

var easyText = SKSpriteNode(imageNamed: "RzLsEasyText.png")
var easyButtonActive = true
var easyButton = UIButton()

var mediumText = SKSpriteNode(imageNamed: "RzLsMediumText.png")
var mediumButtonActive = false
var mediumButton = UIButton()

var hardText = SKSpriteNode(imageNamed: "RzLsHardText.png")
var hardButtonActive = false
var hardButton = UIButton()

var nightmareText = SKSpriteNode(imageNamed: "RzLsNightmareText.png")
var nightmareButtonActive = false
var nightmareButton = UIButton()

class LevelSelect: SKScene {

override func didMoveToView(view: SKView) {

    self.scene?.size = CGSize(width: 1136, height: 640 )

    easyText.position = CGPointMake(980.865, 394)
    easyText.zPosition = 6
    easyButton = UIButton(frame: CGRectMake(974, 240, 100, 64))
    easyButton.addTarget(self, action: "EasyButtonActive", 
    forControlEvents: UIControlEvents.TouchUpInside)

    mediumText.position = CGPointMake(985.603, 338)
    mediumText.zPosition = 5
    mediumButton = UIButton(frame: CGRectMake(974, 240, 178, 64))
    mediumButton.addTarget(self, action: "MediumButtonActive", 
    forControlEvents: UIControlEvents.TouchUpInside)

    hardText.position = CGPointMake(981.833, 292)
    hardText.zPosition = 5
    hardButton = UIButton(frame: CGRectMake(974, 240, 105, 56))
    hardButton.addTarget(self, action: "HardButtonActive", 
    forControlEvents: UIControlEvents.TouchUpInside)

    nightmareText.position = CGPointMake(984.116, 237)
    nightmareText.zPosition = 5

    goBackButton = UIButton(frame: CGRectMake(137.214, 570, 183, 91))
    goBackButton.addTarget(self, action: "GoBack", forControlEvents: 
    UIControlEvents.TouchUpInside)

    previousLevelButton = UIButton(frame: CGRectMake(468.358, 513, 
    62.173, 89))
    previousLevelButton.addTarget(self, action: "MissionDown", 
    forControlEvents: UIControlEvents.TouchUpInside)

    nextLevelButton = UIButton(frame: CGRectMake(667.928, 515.508, 600, 
    600))
    nextLevelButton.backgroundColor = UIColor.blueColor()
    nextLevelButton.addTarget(self, action: "MissionUp", 
    forControlEvents: UIControlEvents.TouchUpInside)

    self.addChild(levelSelectBackground)
    self.addChild(easyText)
    self.addChild(mediumText)
    self.addChild(hardText)
    self.addChild(nightmareText)
    self.addChild(proceedLabel)
    self.addChild(survivalText)
    self.addChild(challangeText)
    self.addChild(LevelScenePreview)
    self.addChild(levelNumber)

    self.view!.addSubview(survivalButton)
    self.view!.addSubview(challangeButton)
    self.view!.addSubview(proceedButton)
    self.view!.addSubview(goBackButton)
    self.view!.addSubview(goBackButton)
    self.view!.addSubview(easyButton)
    self.view!.addSubview(mediumButton)
    self.view!.addSubview(hardButton)
    self.view!.addSubview(nextLevelButton)
    self.view!.addSubview(previousLevelButton)
        }

1 个答案:

答案 0 :(得分:2)

我建议您不要在UIButton中使用UIKitSKView中的任何视图。他们只是不适合。它们使用与精灵节点不同的坐标空间,它们不是节点树的一部分。当场景为paused时,它们仍然可以工作......这只是一团糟。

您应该创建SKSpriteNode s作为按钮。创建按钮纹理并将其放在SKSpriteNode上。覆盖touchesBegantouchesEnded方法以检测点击。如果你有很多按钮,你应该添加一个SKSpriteNode子类。

我为你写了一个子类:

class ButtonNode: SKSpriteNode {
    override init(texture: SKTexture?, color: UIColor, size: CGSize) {
        super.init(texture: texture, color: color, size: size)
        isUserInteractionEnabled = true
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        isUserInteractionEnabled = true
    }

    var onClick: (() -> Void)?

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