2017-02-22 42 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?() 
    } 
}