2016-12-04 1 views
0

ich hinzufügen muß die folgenden View-Controller in meinem SpriteKit Spiel:Wie ein Viewcontroller zu einem SKScene

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

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

Die oben:

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 
    } 

} 

} 

ich es in der folgenden Art und Weise in meinem SKScene versuchte Initialisierung gab mir die folgenden Fehler:

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

und

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

Ich denke, ich mache etwas falsch. Wie kann ich meinen View Controller zum SKScene hinzufügen?

UPDATE

ich ein Menü für mein Spiel zu machen versuchen UICollectionView verwenden, warum ich einen View-Controller oben (jetzt den vollen MenuViewController Code angezeigt) hinzuzufügen, versuche meinen SKScene. Aber jetzt bekomme ich die Idee, dass ich es stattdessen zu meinem SKView hinzufügen sollte. Ich glaube, diese Ansicht kann zu der SKView von meiner SKScene Klasse (unten) hinzugefügt werden. Wie kann das gemacht werden?

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

Haben Sie versucht, self.view? .addSubview (myViewController.view) – nshoute

+0

Ist auch Ihre UIViewController Unterklasse ein Xib oder Storyboard? – nshoute

+0

'self.view? .addSubview (myViewController.view)' hat einen der Fehler behoben. Was ist mit dem anderen - "falsche Argumentbezeichnung im Aufruf (habe 'frame:', erwartet 'coder:')'? Außerdem erstelle ich meinen View-Controller programmatisch, ich bevorzuge diesen Ansatz für das Storyboard. – Serenade

Antwort

-2

Einfache Antwort ist, können Sie nicht. SpriteKit und UIKit sind 2 verschiedene Tiere und werden nur über SKView miteinander verbunden.

+0

Ich denke, was sie zu tun versuchen, ist eine Ebene Auswahlansicht in ihre Spritekit-Spiel wie Flappy Vogels Ebene Auswahlansicht hinzufügen, aber nur eine schlecht formulierte Frage. –

+0

oh ich weiß, was sie zu tun versuchen, deshalb habe ich seine Frage kommentiert – Knight0fDragon

+3

Dies ist die richtige Antwort für die gegebene Frage. Sie können der Szene keine Ansicht hinzufügen. Die Szene wird durch die Ansicht (SKView) dargestellt ... Es sollte wirklich nett sein, dass das Abheben nicht möglich ist, ohne einen Kommentar zu hinterlassen. – Whirlwind

1

Die Antwort war zu lang für einen Kommentar, aber in Bezug auf diese Frage: incorrect argument label in call (have 'frame:', expected 'coder:') ist, weil Sie ein neues MyViewController Objekt erstellen und es mit einem Argument Label frame ob Sie selbst haben, dass als initializer Initialisierung für Ihre Klasse oder nicht ist das Problem.

Wir müssen Ihre MyViewController Klasse sehen und welche Initialisierer Sie eingerichtet haben, weil es scheint, dass Ihre Klasse nur 1 Initialisierer hat, und es scheint init(coder: NSCoder) zu sein. Bitte zeigen Sie uns Ihre MyViewController Klasse.

+0

Wenn Sie einen Kommentar länger als die maximale Länge machen möchten, fügen Sie einfach ... an das Ende des ersten hinzu und fahren Sie mit einem zweiten Kommentar fort. Ich denke, das wäre besser als Kommentar gedient – Nik

+0

Ich habe gerade die Frage aktualisiert – Serenade

Verwandte Themen