2017-02-12 39 views
1

我在`SKScene'中有一個菜單。我正在嘗試將Peek和Pop功能集成到它中。我有多種可能的選擇,所以我的問題是如何確定哪一個正在被觸摸。Peek和Pop函數SpriteKit

這是我的代碼:

import SpriteKit 
import UIKit 

class Menu: SKScene, UIViewControllerPreviewingDelegate { 

//SETUP Menu 


func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? { 
     return UIViewController() 
    } 

    //POP 
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) { 
     return 
    } 

} 

GameViewController.swift:

import UIKit 
import SpriteKit 

class GameViewController: UIViewController { 


    override func viewDidLoad() { 
    super.viewDidLoad() 

    if let scene = GameScene(fileNamed:"GameScene") { 
     if self.traitCollection.forceTouchCapability == .available { 
      registerForPreviewing(with: scene as! UIViewControllerPreviewingDelegate, sourceView: scene.view!) 
      } 
     } 

     // Move to Menu 

} 
} 

我敢肯定,我要傳遞的信息在sourceView領域。但由於這是在GameViewController中,我不知道該怎麼做。而且我不知道如何在菜單內做到這一點,我會在哪裏放這個代碼?

if let scene = GameScene(fileNamed:"GameScene") { 
      if self.traitCollection.forceTouchCapability == .available { 
       registerForPreviewing(with: scene as! UIViewControllerPreviewingDelegate, sourceView: ButtonPressed) 
      } 
     } 

回答

0

您將無法使用皮克和流行的SKScene或SKSpriteNode菜單像你想,我試圖做同樣的在我的遊戲之一。

Peek和Pop僅適用於UIKit,特別是它允許您預覽(Peek)和打開(Pop)UIViewControllers。

在SpriteKit中,通常只有1個ViewController,GameViewController。該GameViewController應該只處理您的SKScene演示文稿,您的實際UI應該直接在相關SKScenes中使用SpriteKit API完成,例如SKLabelNodes,SKSpriteNodes等。因此,您不會真正使用UIKit,並且沒有太多可以使用Peek和Pop預覽。

有一些例外,所以這取決於你正在做什麼樣的遊戲。例如,在我的一個遊戲中,我爲我的關卡選擇菜單(40關卡)使用了UICollectionViewController,因爲它們非常適合該場景(單元重用,平滑滾動等)。由於該菜單是由UIKit製作的,因此當您強制觸摸單元格時,我可以註冊窺視和彈出。我比另一個UIViewController與一些UILabels,UIImageViews等我專門用於偷看(見圖片),當我彈出我只是開始實際的水平。

所以總之,在SpriteKit中可以使用Peek和Pop的情況並不多。我也不會推薦嘗試用UIKIt設計你的UI,以便你可以使用Peek和Pop。希望Apple有一天能夠更新API,以便能夠在SKNodes或SKScenes中查看並彈出時,這會非常棒。

希望這有助於

enter image description here

+0

謝謝,可惜我不能重新設計的菜單,因爲它是一個遊戲,必須SpriteKit進行。 –

+0

不客氣。我不會重新設計你的菜單,它使用SpriteKit中的UIKit笨重(除了可能的ColletionViews,ScrollViews)。如果您還沒有這麼做,請查看3D touch快速操作,它們可以輕鬆集成到SpriteKit中。快樂編碼 – crashoverride777

+0

是的,我有快速行動 –