2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Gameテンプレートを使用せず、横向きゲームアプリを作成する

2
Last updated at Posted at 2015-03-13

アプリの準備

  • Single View Applicationで新規Projectを作成する
  • ViewControllerを修正する
ViewController.swift
import UIKit
import SpriteKit

class ViewController: UIViewController {
    override func viewWillAppear(animated: Bool){
        let skView = self.view as SKView
        let scene = GameScene(size: skView.bounds.size)
        
        skView.presentScene(scene)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let skView = self.view as SKView
        skView.showsFPS = true
        skView.showsNodeCount = true
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
} 
  • GameScene作成
GameScene.swift
import SpriteKit

class GameScene: SKScene{
    
}
  • storyboardのインスペクタからSKViewクラスに設定する
  • 実行して、FPSとノード数が表示されていることを確認する

iphone.png

横向き対応

  • 設定を横向きにする(Portraitのチェックを外す)
    portrait.png
  • 上記設定を変更するだけでは、縦向きの状態でシーンが生成されてしまっているため、viewDidLoadからviewWillLayoutSubviewsに修正する
ViewController.swift
import UIKit
import SpriteKit

class ViewController: UIViewController {
    var viewInitiated: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillLayoutSubviews() {
        if(!viewInitiated){
            super.viewWillLayoutSubviews()
            let skView = self.view as SKView
            skView.showsFPS = true
            skView.showsNodeCount = true
            let scene = GameScene(size: skView.bounds.size)

            skView.presentScene(scene)
            self.viewInitiated = true
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
}

iphone_yoko.png

参考

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?