行うこと
- ボタン作成
- touchesBeganでタッチ判定
- アクション(animation)
- 画面遷移
実装
- 1 Scene1.swift作成
- 2 Scene2.swift作成
- 3 GameViewControllerの書き換え
import SpriteKit
import GameplayKit
extension UIColor {
convenience init(hex: UInt, alpha: CGFloat = 1.0) {
let red: CGFloat = CGFloat((hex & 0xFF0000) >> 16) / 255.0
let green: CGFloat = CGFloat((hex & 0x00FF00) >> 8) / 255.0
let blue: CGFloat = CGFloat(hex & 0x0000FF) / 255.0
self.init(red: red, green: green, blue: blue, alpha: alpha)
}
}
class Scene1: SKScene {
override func didMove(to view: SKView) {
self.backgroundColor = UIColor.white
let button = self.createSquare("button")
button.run(self.actionSequence(), completion: {
self.transition()
})
self.addChild(button)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first as UITouch? {
let location = touch.location(in: self)
if self.atPoint(location).name == "button" {
self.transition()
}
}
}
func transition() {
let transition = SKTransition.push(with: .left, duration: 1.0)
let scene = Scene2(size: self.view!.frame.size)
self.view!.presentScene(scene, transition: transition)
}
func action() -> SKAction {
return SKAction.moveBy(x: 0, y: 100, duration: 1.0)
}
func actionSequence() -> SKAction {
let moveUp = SKAction.moveBy(x: 0, y: 100, duration: 1.0)
let zoom = SKAction.scale(to: 2.0, duration: 0.25)
let wait = SKAction.wait(forDuration: 0.5)
let fadeAway = SKAction.fadeOut(withDuration: 0.25)
let removeNode = SKAction.removeFromParent()
let sequence = SKAction.sequence(([moveUp, zoom, wait, fadeAway, removeNode]))
return sequence
}
func createLabel(_ name: String) -> SKLabelNode {
let label = SKLabelNode(fontNamed: "Chalkduster")
label.text = "Hello World!"
label.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
label.fontSize = CGFloat(30)
label.name = name
return label
}
func createSquare(_ name: String) -> SKShapeNode {
let square = SKShapeNode(rectOf: CGSize(width: 150, height: 100))
square.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
square.fillColor = UIColor(hex: 0x008678)
square.name = name
return square
}
}
import SpriteKit
import GameplayKit
class Scene2: SKScene {
override func didMove(to view: SKView) {
self.backgroundColor = UIColor.black
let button = self.createSquare("button")
button.run(self.action(), completion: {
self.transition()
})
self.addChild(button)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first as UITouch? {
let location = touch.location(in: self)
if self.atPoint(location).name == "button" {
self.transition()
}
}
}
func createSquare(_ name: String) -> SKShapeNode {
let square = SKShapeNode(rectOf: CGSize(width: 150, height: 100))
square.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
square.fillColor = UIColor(hex: 0xee0026)
square.strokeColor = UIColor.clear
square.name = name
return square
}
func transition() {
let transition = SKTransition.push(with: .left, duration: 1.0)
let scene = Scene1(size: self.view!.frame.size)
self.view!.presentScene(scene, transition: transition)
}
func action() -> SKAction {
return SKAction.moveBy(x: 0, y: 100, duration: 1.0)
}
}
import UIKit
import SpriteKit
import GameplayKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
let scene = Scene1(size: view.frame.size)
view.presentScene(scene)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override var prefersStatusBarHidden: Bool {
return true
}
}