LoginSignup
0
0

More than 5 years have passed since last update.

[tvOS]Gameコントローラ

Posted at

tvOSのサンプルDemoBotsとGameplayKitのサンプルMazeを参考にして、SpriteKitとGameplayKitを使ったtvOSのゲーム・アプリケーションの実装を試す。

Xcodeの新規プロジェクトで生成されるSpriteKitを使ってプロジェクトをGameコントローラを使用したものに変更してみる。

Gameコントローラの内容は以下のとおり。

import Foundation
import SpriteKit
import GameplayKit
 
class Game {
    var gameScene: GameScene?
    let random: GKRandomSource?
    
    init() {
        random = GKRandomSource()
    }
    
    deinit {
    }
    
    var scene: GameScene? {
        get {
            if gameScene == nil {
                gameScene = GameScene(fileNamed: "GameScene")
            }
            return gameScene
        }
    }
}

以下がViewControllerの変更前。

class GameViewController: UIViewController {
    :
    override func viewDidLoad() {
        print(NSStringFromClass(self.dynamicType), __FUNCTION__)
        super.viewDidLoad()
        
        if let scene = GameScene(fileNamed: "GameScene") {
            // Configure the view.
            let skView = self.view as! SKView
            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 = .AspectFill
            
            skView.presentScene(scene)
        }
    }
    :
}

以下がViewControllerの変更後。

class GameViewController: UIViewController {
    :
    override func viewDidLoad() {
        print(NSStringFromClass(self.dynamicType), __FUNCTION__)
        super.viewDidLoad()
        
        game = Game()
        if let aGame = game {
            let scene = aGame.scene
            scene!.scaleMode = .AspectFill
            
            let skView = view as! SKView
            skView.presentScene(scene)
            skView.ignoresSiblingOrder = true
            skView.showsFPS = true
            skView.showsNodeCount = true
        }
    }
    :
}

ソースコード
GitHubからどうぞ。

https://github.com/murakami/workbook/tree/master/tvos/Pokopen - GitHub

関連情報
Cocoa Advent Calendar 2015

App Programming Guide for tvOS

GameplayKit Programming Guide

実践!iOSで作るゲームアプリ

【Cocoa練習帳】
http://www.bitz.co.jp/weblog/

http://ameblo.jp/bitz/(ミラー・サイト)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0