2017-02-22 49 views
0

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

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) 
     } 
+0

你试过用zPositions乱搞? – sicvayne

+0

@sicvayne Ui按钮没有z位置只SkNodes做 –

回答

2

我建议你不要在SKView使用UIButton S或任何视图UIKit。他们只是不适合。它们使用与sprite节点不同的坐标空间,它们不是节点树的一部分。当场景是paused时,他们仍然工作......这只是一团糟。

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