More Related Content Similar to Introduction of Swift from Game Development
Similar to Introduction of Swift from Game Development (20) More from Daisuke Yamashita
More from Daisuke Yamashita (15) Introduction of Swift from Game Development19. Step3 GemaSceneの登録
let scene = GameScene()
let view = self.view as! SKView
view.showsFPS = true
view.showsNodeCount = true
scene.size = view.frame.size
view.presentScene(scene)
・GameViewControllerのviewDidLoadに
20. Step4 ノードの表示と物理効果
let ball = SKSpriteNode(imageNamed: “ball”)
ball.physicsBody = SKPhysicsBody(texture:
SKTexture(imageNamed:"ball"), size: ball.size)
ball.position = CGPoint(x:100 ,y:100)
self.addChild(ball)
・GameSceneのdidMoveToViewに
ボール、バー、ボードを表示するように設定していきます。
21. Step5 Swiftのちょっとした文法
class Grape:SKSpriteNode {
var hitCount:Int = 0
}
・クラスの継承
・データ構造
let grapesPosition:[(x:CGFloat,y:CGFloat)] = [(60.0,500.0),
(160.0,500.0),(260.0,500.0),(110.0,400.0),(220.0,400.0)]
配列、タプル
・繰り返し
//グレープの配置
for pos in grapesPosition{
makeGrape(pos)
}
23. Step7 タッチ系イベントハンドリング
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch: AnyObject = touches.first {
let location = touch.locationInNode(self)
let action = SKAction.moveTo(CGPoint(x: location.x, y: 100), duration: 0.2)
self.board.runAction(action)
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch: AnyObject = touches.first {
if !isStarted{
startBall()
isStarted = true
}
}
}
24. Step8 衝突判定
self.physicsWorld.contactDelegate = self
extension GameScene:SKPhysicsContactDelegate{
func didBeginContact(contact: SKPhysicsContact) {
}
}
delegateの設定
以下のメソッドを追加することで衝突時に呼ばれる
※contactTestBitMaskとcategoryBitMaskの論理積が0以外の
値になると衝突判定
26. Step9 パーティクル
let particle = SKEmitterNode(fileNamed: "MyParticle.sks")
self.addChild(particle)
let removeAction = SKAction.removeFromParent()
let durationAction = SKAction.waitForDuration(1)
let sequenceAction = SKAction.sequence([durationAction,removeAction])
particle.runAction(sequenceAction)
particle.position = CGPoint(x: g.position.x, y: g.position.y)
particle.alpha = 1