6
6

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.

SpriteKitを試してみる6 キャラをクラス化と無敵時間

Posted at

iphonePlay_20150402.gif

ダメージを受けた数秒間を点滅させて、無敵時間にする
その為にまずベースとなるキャラのクラスを作成

import Foundation
import SpriteKit

class BaseUnit: SKSpriteNode {
    var hitPoint : Int = 0
    var attackPoint : Int = 0
}

次にプレイヤーが操作するドラゴンのクラスをベースを元に作成。ここでstatusというプロパティを設定。

import Foundation
import UIKit
import SpriteKit

class DragonUnit: BaseUnit {
    var status : String = ""
    
    override init() {
        
        let texture = SKTexture(imageNamed: "charImage1.png")
        
        super.init(texture: texture, color: nil, size: texture.size())
        
        hitPoint = 40
    }
    
    required override init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

もうひとつ敵のクラスも作成

import Foundation
import UIKit
import SpriteKit

class EnemyUnit: BaseUnit {
    override init() {
        
        let random : Int = Int(arc4random() % 4)
        var color : UIColor!
        
        switch random {
        case 0 :
            color = UIColor.redColor()
        case 1 :
            color = UIColor.yellowColor()
        case 2 :
            color = UIColor.greenColor()
        case 3 :
            color = UIColor.brownColor()
        default :
            color = UIColor.blueColor()
        }
        
        super.init(texture: nil, color: color, size: CGSizeMake(50, 50))

        hitPoint = 0
        attackPoint = 10

    }
    
    required override init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}

GameSceneクラスでドラゴンを呼び出す箇所を修正

  override func didMoveToView(view: SKView) {
          //キャラ画像
//        let dragonImage = UIImage(named: "charImage1.png")
//        let texture = SKTexture(image: dragonImage!)
//        let dragon = SKSpriteNode(texture: texture)

        let dragon = DragonUnit()

衝突時のイベントで敵とドラゴンがぶつかった時を修正

    //衝突時のイベント
    func didBeginContact(contact: SKPhysicsContact) {
    
           // キャラと敵が衝突した場合
        if((CollisionMember.Dragon.toInt() == contact.bodyA.categoryBitMask ||
            CollisionMember.Dragon.toInt() == contact.bodyB.categoryBitMask ) &&
            (CollisionMember.Enemy.toInt() == contact.bodyA.categoryBitMask ||
                CollisionMember.Enemy.toInt() == contact.bodyB.categoryBitMask)){
                    
                    // ダメージを点滅で表現
                    let dragon: DragonUnit = childNodeWithName("dragon") as DragonUnit
                    
                    if (dragon.status != "damaged"){
                        dragon.status = "damaged"
                        
                        let enemy: EnemyUnit = childNodeWithName("Enemy") as EnemyUnit
                        dragon.hitPoint -= enemy.attackPoint
                        
                        // 点数を減算
                        
//                        let myPointLabel: SKLabelNode = childNodeWithName("pointLabel") as SKLabelNode
//                        myPointLabel.text = String(myPoint)
//                       
                        if(dragon.hitPoint > 0){
                            myLabel.text = "Ouch!"
                            
                        // ドラゴンの点滅
                            let blank = SKAction.sequence([SKAction.fadeOutWithDuration(0.2),
                                SKAction.fadeInWithDuration(0.2)])
                            let repeat = SKAction.repeatAction(blank, count: 10)
                            dragon.runAction(repeat, completion:{
                                dragon.status = ""
                                self.myLabel.text = "Hello World"
                                
                            })
                        } else {
                            myLabel.text = "Game Over!Good bye"
                            
                        }
                    }
                    
                    
                    // 敵の方を消す
                    if(contact.bodyA.categoryBitMask == CollisionMember.Enemy.toInt()){
                        contact.bodyA.node?.removeFromParent()
                    } else {
                        contact.bodyB.node?.removeFromParent()
                    }
        }        

ぶつかった時にstatusにdamagedをセット。点滅のイベントが終了時にstatusにブランクをセットするまでダメージを負わない。ダメージを受けたら敵の攻撃力でHPを減らす。0以下になったらゲームオーバになるけど、今のところそれに対応したイベントはなし。

次はスタート画面と終了画面を表示するところに挑戦。

なお、今回は他にもドラゴンの移動方法の変更や火炎を吐くのを自動に変更、敵キャラをランダムに出現させるといった事以外にも敵を撃破したら点数を出すといった事をしているがそこは割愛。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?