私は、spritekitではじめる2Dゲームプログラミングという本をつかっているのですが、本のバージョンが古いためか動かないことが多々あります。私がわからないのは積み上げゲームのところで、SKViewをアップキャストするところでいつも
Could not cast value of type 'UIView' (0x1dea549c8) to 'SKView' (0x1dd4061e0).
となり入れることができません。
↓ViewController.swift
import UIKit
import SpriteKit
class ViewController: UIViewController{
override func viewDidLoad() { super.viewDidLoad() let scene = GameScene() let view = self.view as! SKView **//ここで Thread 1: signal SIGABRT とでてしまいます。** view.showsFPS = true view.showsNodeCount = true scene.size = view.frame.size view.presentScene(scene) }
}
↓GameScene.swift
import Foundation
import SpriteKit
class GameScene : SKScene, SKPhysicsContactDelegate {
// どんぶり用スプライト (画像)
var bowl:SKSpriteNode?
// 落下用タイマー var timer:Timer? // 落下判定用シェイプ (図形) var lowestShape:SKShapeNode? // スコア var score = 0 // スコア用ラベル (文字) var scoreLabel: SKLabelNode? // 名古屋名物ごとのスコア var scoreList = [100, 200, 300, 500, 800, 1000, 1500] override func didMove(to view: SKView) { // 下方向への重力を設定 self.physicsWorld.gravity = CGVector(dx: 0.0, dy: -2.0) self.physicsWorld.contactDelegate = self // 背景画像のスプライトを配置 let background = SKSpriteNode(imageNamed: "background") background.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.5) background.size = self.size self.addChild(background) // 落下判定用シェイプ let lowestShape = SKShapeNode(rectOf: CGSize(width: self.size.width*3, height: 10)) lowestShape.position = CGPoint(x: self.size.width*0.5, y: -10) // 画面外に配置 let physicsBody = SKPhysicsBody(rectangleOf: lowestShape.frame.size) // シェイプの大きさで物理シミュレーションを行う physicsBody.isDynamic = false // 落下しないよう固定 physicsBody.contactTestBitMask = 0x1 << 1 // 名古屋名物との衝突を検知する lowestShape.physicsBody = physicsBody self.addChild(lowestShape) self.lowestShape = lowestShape // どんぶり用スプライト let bowlTexture = SKTexture(imageNamed: "bowl") let bowl = SKSpriteNode(texture: bowlTexture) bowl.position = CGPoint(x: self.size.width*0.5, y: 100) bowl.size = CGSize(width: bowlTexture.size().width*0.5, height: bowlTexture.size().height*0.5) bowl.physicsBody = SKPhysicsBody(texture: bowlTexture, size: bowl.size) // テクスチャの不透過部分の形状で物理シミュレーションを行う bowl.physicsBody?.isDynamic = false // 落下しないよう固定 self.bowl = bowl self.addChild(bowl) // スコア用ラベル let scoreLabel = SKLabelNode(fontNamed: "Helvetica") scoreLabel.position = CGPoint(x: self.size.width * 0.92, y: self.size.height * 0.78) scoreLabel.text = "¥0" scoreLabel.fontSize = 32 scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.right // 右寄せ scoreLabel.fontColor = UIColor.green self.addChild(scoreLabel) self.scoreLabel = scoreLabel // 名古屋名物を1つ落下させる self.fallNagoyaSpecialty() // タイマーを作成し一定時間ごとにfallNagoyaSpecialtyメソッドを呼ぶ self.timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: Selector(("fallNagoyaSpecialty")), userInfo: nil, repeats: true) } // 名古屋名物を落下させるメソッド func fallNagoyaSpecialty() { // 0~6のランダムな整数を発生させる let index = Int(arc4random_uniform(7)) let texture = SKTexture(imageNamed: "(index)") // 選択された番号のテクスチャを読み込む // テクスチャからスプライトを生成する let sprite = SKSpriteNode(texture: texture) sprite.position = CGPoint(x: self.size.width*0.5, y: self.size.height) sprite.size = CGSize(width: texture.size().width * 0.5, height: texture.size().height * 0.5) sprite.physicsBody = SKPhysicsBody(texture: texture, size: sprite.size) // テクスチャの不透過部分の形状で物理シミュレーションを行う sprite.physicsBody?.contactTestBitMask = 0x1 << 1 // 落下判定用シェイプとの衝突を検知する self.addChild(sprite) // 落下した物に応じてスコアを加算する self.score += self.scoreList[index] // 金額のラベルを更新 self.scoreLabel?.text = "¥(self.score)" } // タッチ開始時に呼ばれるメソッド override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch: AnyObject = touches.first { let location = touch.location(in: self) let action = SKAction.move(to: location, duration: 0.2) self.bowl?.run(action) } } // 指を動かしたときに呼ばれるメソッド override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch: AnyObject = touches.first { // シーン上のタッチされた位置を取得する let location = touch.location(in: self) // タッチされた位置にノードを移動させるアクションを作成する let action = SKAction.move(to: CGPoint(x: location.x, y: 100), duration: 0.2) // どんぶりのスプライトでアクションを実行する self.bowl?.run(action) } } // 衝突が発生したときに呼ばれるメソッド func didBeginContact(contact: SKPhysicsContact) { // 衝突した一方が落下判定用シェイプだったら if contact.bodyA.node == self.lowestShape || contact.bodyB.node == self.lowestShape { // ゲームオーバースプライトを表示 let sprite = SKSpriteNode(imageNamed: "gameover") sprite.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.5) self.addChild(sprite) // アクションを停止させる self.isPaused = true // タイマーを止める self.timer?.invalidate() }
}
}
SKViewDelegateでやれるかと思って試してみましたが、自分ではよくわかりませんでした。
初心者ですけれどもよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/14 17:58