swift - Ui 按钮未添加到 SkScene

标签 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)
        }

最佳答案

我建议您不要在 SKView 中使用 UIButtonUIKit 中的任何 View 。他们只是不适合。它们使用与 Sprite 节点不同的坐标空间,并且它们不是节点树的一部分。当场景 paused 时,它们仍然工作......只是一团糟。

您应该创建 SKSpriteNode 作为按钮。创建按钮纹理并将它们放在 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?()
    }
}

关于swift - Ui 按钮未添加到 SkScene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42383413/

相关文章:

ios - 如何快速调整大图像的大小并使其适合 UIButton?

ios - UIScrollView 需要根据 UILabel 大小进行扩展,但保持底部标签栏

objective-c - XCode 6.4 : Objective-C classes are not visible to code completion inside Swift 1. 2 文件

javascript - 系统对话框 Angular Material

java - jButton 立即点击

objective-c - 如何在标题更改时停止不需要的 UIButton 动画?

ios - 解析/Swift 初学者 : How to change label on Parse query

ios - swift if-let 不会打开可选的 NSDictionary

java - 在使用 libgdx 中的 scene2d 按钮时需要帮助

ios - 禁用带有 textFieldDidBeginEditing 的 UIButton 需要单击 UITextField 才能生效