2016-12-04 63 views
0

我在SpriteKit遊戲下面的視圖控制器:如何將視圖控制器添加到SKScene

import UIKit 

class MenuViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 

lazy var collectionView: UICollectionView = { 
    let layout = UICollectionViewFlowLayout() 
    layout.scrollDirection = .horizontal 
    layout.minimumLineSpacing = 0 
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) 
    cv.backgroundColor = .red 
    cv.dataSource = self 
    cv.delegate = self 
    cv.isPagingEnabled = true 
    return cv 
}() 


let cellId = "cellId" 

override func viewDidLoad() { 
    super.viewDidLoad() 

    view.addSubview(collectionView) 

    //use autolayout instead 
    collectionView.anchorToTop(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor) 

    collectionView.register(PageCell.self, forCellWithReuseIdentifier: cellId) 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 4 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath as IndexPath) 

    return cell 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return CGSize(width: view.frame.width, height: view.frame.height) 
    } 

} 

extension UIView { 

func anchorToTop(top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil) { 

    anchorWithConstantsToTop(top: top, left: left, bottom: bottom, right: right, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0) 
} 

func anchorWithConstantsToTop(top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0) { 

    translatesAutoresizingMaskIntoConstraints = false 

    if let top = top { 
     topAnchor.constraint(equalTo: top, constant: topConstant).isActive = true 
    } 

    if let bottom = bottom { 
     bottomAnchor.constraint(equalTo: bottom, constant: -bottomConstant).isActive = true 
    } 

    if let left = left { 
     leftAnchor.constraint(equalTo: left, constant: leftConstant).isActive = true 
    } 

    if let right = right { 
     rightAnchor.constraint(equalTo: right, constant: -rightConstant).isActive = true 
    } 

} 

} 

我試着在我的SKScene初始化它以下列方式:

override init(size: CGSize){ 
    super.init(size: size) 
    weak var myViewController: MyViewController! 

    myViewController = MyViewController(frame: self.frame) 
    self.view?.addSubview(myViewController)  
} 

以上給了我以下錯誤:

"incorrect argument label in call (have 'frame:', expected 'coder:')" 

"cannot convert type of 'MyViewController!' to expected argument type 'UIView'" 

我想我做錯了什麼。如何將我的視圖控制器添加到SKScene

UPDATE

我試圖讓使用UICollectionView,這就是爲什麼我試圖以上(現在顯示的全MenuViewController代碼)一個視圖控制器添加到我的SKScene我的遊戲菜單。但是現在我明白了,我應該將它添加到我的SKView中。我相信這個觀點可以從我的SKScene課程(如下)添加到SKView。如何才能做到這一點?

import SpriteKit 
import GameplayKit 
import UIKit 

class GameScene: SKScene { 

private var label : SKLabelNode? 
private var spinnyNode : SKShapeNode? 



override func didMove(to view: SKView) { 



} 


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let label = self.label { 
     label.run(SKAction.init(named: "Pulse")!, withKey: "fadeInOut") 
    } 

} 

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

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

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

} 


override func update(_ currentTime: TimeInterval) { 
    // Called before each frame is rendered 
} 


} 
+0

您是否嘗試過self.view?.addSubview(myViewController.view) – nshoute

+0

另外,您的UIViewController子類是xib還是storyboard? – nshoute

+0

'self.view?.addSubview(myViewController.view)'已經解決了其中一個錯誤。另一個呢 - 調用中不正確的參數標籤(有'框架:',預期'編碼器:')'?此外,我正在編程創建我的視圖控制器,我更喜歡這種方法的故事板。 – Serenade

回答

-2

簡單的答案是你不能。 SpriteKit和UIKit是兩種不同的動物,它們只通過SKView連接在一起。

+0

我認爲他們想要做的就是將一個關卡選擇視圖添加到他們的spritekit遊戲中,就像飛鳥的等級選擇視圖等一樣,但只是一個措辭不良的問題。 –

+0

哦,我知道他們正在嘗試做什麼,這就是爲什麼我評論他的問題 – Knight0fDragon

+3

這是給定問題的正確答案。您無法將視圖添加到場景中。場景由視圖(SKView)呈現......如果不發表評論,則不可能進行downvoting。 – Whirlwind

1

答案是太長了評論,但關於這個問題:incorrect argument label in call (have 'frame:', expected 'coder:')是因爲你正在創建一個新的MyViewController對象,並與frame參數標籤初始化它無論你連自己有作爲一個初始的上課還是不上課是問題。

我們需要查看您的MyViewController類以及您設置了哪些初始值設定項,因爲您的類似乎只有1個初始值設定項,並且它似乎是init(coder: NSCoder)。 請向我們展示您的MyViewController課程。

+0

如果您想發表評論的時間超過最大長度,只需將...添加到第一個的結尾,然後繼續第二個評論。我認爲這會更好地作爲評論 – Nik

+0

我剛剛更新了問題 – Serenade

相關問題