Swiftで遊ぼう! on Hatena

あしたさぬきblogでやってた初心者オヤジのiOSプログラミング奮闘記がHatenaに来ました

Swiftで遊ぼう! - 889 - 敵を加えていく段階です

VisualComponentクラスを使って画面に表示する機能を付け加えた... とは思わない自分もいるけど、表示機能を加えたとしよう。

次はEntityオブジェクトに動きを加えていきます。

新しいComponentクラスを作ります。またメニューから「File -> New -> File...」を選び、「Cocoa Touch Class」にしてから、「Subclass of:」に「VisualComponent」を入力してから名前を「MovementComponent」にしてプロジェクト内に保存します。

VisualComponentクラスを継承しているところが解せないんですが、「動き」は、表示されているから機能するわけで、描画機能を継承してもいいという説明があります。ゲームに使用される全てのオブジェクトに共通機能なので、これでいいのでしょう。

MovementComponentクラスに必要なプロパティは、たった一つだけです。動いていくゴールの座標情報だけです。

var destination: int2!

では、初期化ステップをコードします。

init(scene: GameScene, 
         sprite: SKSpriteNode, 
     coordinate: int2,
    destination: int2) {
        
    self.destination = destination
        
    super.init(scene: scene, 
              sprite: sprite, 
          coordinate: coordinate)
}
    
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

ここまでのコーディングはVisualComponentクラスと同じですね。ここから移動のためのメソッドを導入していきます。

これだけ(^^;)