2016-04-26 9 views
0

Also versuche ich Spiel-Center zu meinem Spiel hinzufügen, aber da, dass es in spriteKit es nicht funktionieren wird ..Integrieren Game Center - werfen Wert vom Typ ‚UIView‘ zu SKView“Fehler

i gesucht haben und auf der Suche und evrything weist darauf hin, dass es nur möglich in einem UIViewController ist ..

all meine Knöpfe kaufen (wie die Taste, um Highscore zu gehen (Rangliste) ist in spriteKit ...

so jetzt habe ich die Fehler

Could not cast value of type 'UIView' (0x3755c3d8) to 'SKView' (0x36e116cc). 

Das ist mein GameViewController (Die einzige Datei mit einem UIViewController), aber es wird verwendet, um zu zeigen, mir nur die TitleScene

class GameViewController: UIViewController, GKGameCenterControllerDelegate { 

var gameCenterViewController = GKGameCenterViewController() 
var scene : TitleScene? 
var gcEnabled = Bool() 
var gcDefaultLeaderBoard = String() 


func authenticateLocalPlayer() { 

    let localPlayer: GKLocalPlayer = GKLocalPlayer.localPlayer() 

localPlayer.authenticateHandler = {(GameViewController, error) -> Void in 
    if((GameViewController) != nil) { 
     // 1 Show login if player is not logged in 
     self.presentViewController(GameViewController!, animated: true, completion: nil) 
    } else if (localPlayer.authenticated) { 
     // 2 Player is already euthenticated & logged in, load game center 
     self.gcEnabled = true 

     // Get the default leaderboard ID 
     localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler({ (leaderboardIdentifer: String?, error: NSError?) -> Void in 
      if error != nil { 
       print(error) 
      } else { 
       self.gcDefaultLeaderBoard = leaderboardIdentifer! 
      } 
     }) 


    } else { 
     // 3 Game center is not enabled on the users device 
     self.gcEnabled = false 
     print("Local player could not be authenticated, disabling game center") 
     print(error) 
    } 

} 

} 

func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController) { 
    gameCenterViewController.dismissViewControllerAnimated(true, completion: nil) 
    } 
override func viewDidLoad() { 
    super.viewDidLoad() 

let skView = self.view as! SKView 

self.authenticateLocalPlayer() 

scene = TitleScene(size: skView.bounds.size) 
// Configure the view. 

skView.showsFPS = true 
skView.showsNodeCount = true 

/* Sprite Kit applies additional optimizations to improve rendering performance */ 
skView.ignoresSiblingOrder = true 

/* Set the scale mode to scale to fit the window */ 
scene!.scaleMode = SKSceneScaleMode.AspectFit 
scene!.size = self.view.bounds.size 



skView.presentScene(scene) 
} 

und in meinem TitleScene bekam ich den

class TitleScene: SKScene { 

if highScoreBtn.containsPoint(location) { 
      let ScoreleaderboardID = "ScoreLeaderboardID" 
      let sScore = GKScore(leaderboardIdentifier: ScoreleaderboardID) 
      sScore.value = Int64(score) 

      let localPlayer: GKLocalPlayer = GKLocalPlayer.localPlayer() 

      let gcVC: GKGameCenterViewController = GKGameCenterViewController() 
      gcVC.gameCenterDelegate = GameViewController() 
      gcVC.viewState = GKGameCenterViewControllerState.Leaderboards 
      gcVC.leaderboardIdentifier = "LeaderboardID" 
      self.view?.window?.rootViewController?.presentViewController(GameViewController(), animated: true, completion: nil) 

      GKScore.reportScores([sScore], withCompletionHandler: { (error: NSError?) -> Void in 
       if error != nil { 
        print(error!.localizedDescription) 
       } else { 
        print("Score submitted") 

       } 
      }) 


      print("highScoreBtn") 
     } 

Antwort

0

Oke vergeßt fand ich es .

Heres die Lösung für mein Problem.

in GameViewController

class GameViewController : UIViewController { 

var scene : TitleScene? 

// evrythong moved to TitleScene 

} 

und wie dies in Ihrem TitleScene.

class TitleScene :SKScene, GKGameCenterControllerDelegate { 

    var gameCenterViewController = GKGameCenterViewController() 
    var gcEnabled = Bool() 
    var gcDefaultLeaderBoard = String() 

    func authenticateLocalPlayer() { 
     let localPlayer: GKLocalPlayer = GKLocalPlayer.localPlayer() 

     localPlayer.authenticateHandler = {(TitleScene, error) -> Void in 
      if((TitleScene) != nil) { 
       // 1 Show login if player is not logged in 


// here it was original self.presentViewController(...) 
// had to add self.view?.window?.rootController? to it 

self.view?.window?.rootViewController?.presentViewController(TitleScene!, animated: true, completion: nil) 
       } else if (localPlayer.authenticated) { 
        // 2 Player is already euthenticated & logged in, load game center 
        self.gcEnabled = true 



    // Get the default leaderboard ID 
        localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler({ (leaderboardIdentifer: String?, error: NSError?) -> Void in 
         if error != nil { 
          print(error) 
         } else { 
          self.gcDefaultLeaderBoard = leaderboardIdentifer! 
         } 
        }) 


      } else { 
       // 3 Game center is not enabled on the users device 
       self.gcEnabled = false 
       print("Local player could not be authenticated, disabling game center") 
       print(error) 
      } 

     } 

    } 

    func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController) { 
     gameCenterViewController.dismissViewControllerAnimated(true, completion: nil) 
    } 
} 

didMoveToView Funktion:

override func didMoveToView(view: SKView) { 

    // in the didMoveToView you add the function 

    self.authenticateLocalPlayer() 

} 

und in der Funktion der Rang

  if highScoreBtn.containsPoint(location) { 
      let ScoreleaderboardID = "ScoreLeaderboardID" 
      let sScore = GKScore(leaderboardIdentifier: ScoreleaderboardID) 
      sScore.value = Int64(score) 

      let localPlayer: GKLocalPlayer = GKLocalPlayer.localPlayer() 

      let gcVC: GKGameCenterViewController = GKGameCenterViewController() 
      gcVC.gameCenterDelegate = self 
      gcVC.viewState = GKGameCenterViewControllerState.Leaderboards 
      gcVC.leaderboardIdentifier = "LeaderboardID" 

//here i did the same as in with the other self.presentViewController 

self.view?.window?.rootViewController?.presentViewController(gcVC, animated: true, completion: nil) 

      GKScore.reportScores([sScore], withCompletionHandler: { (error: NSError?) -> Void in 
       if error != nil { 
        print(error!.localizedDescription) 
       } else { 
        print("Score submitted") 

       } 
      }) 


      print("highScoreBtn") 
     } 
zu gehen