1
0

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学習のメモ(アクションの設定)

Last updated at Posted at 2018-11-11

アクションを用いてスプライトに動きをつける

アクション(SKAction)とは?

ノード(Node)に動きをつけたい場合に、アクションを使う。アクションは、アクションを適用したノードの位置や回転、スケールなどといったプロパティを段階的に変化させることで実現している。

基本的な使い方

1、メソッドを使ってSKActionのインスタンスを生成する
2、アニメーションさせたいSKNodeのrunActionメソッドに、SKActionのインスタンスを渡す
(ex)基本的な使い方↓

Sample.swift
// sample.pngイメージを生成
var player = SKSpriteNode(imageNamed: "sample")

// 1秒かけてサイズを2倍にするSKActionを生成
let act = SKAction.scale(to: 2.0, duration:1.0)

// 対象ノードに渡して実行
player.runAction(act)

SKActionクラスには、**「動きをつけるメソッド」「効果をつけるメソッド」「拡大・縮小を行うメソッド」「透過を行うメソッド」「順序制御を行うメソッド」**などもある。
 これらのサンプルをもとに、宇宙船からレーザー光線が発射されるアクションを実装↓

GameScene.swift
  override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        // タッチ終了後に定数missileでheroからのみレーザー光線を宣言
        let laser = SKSpriteNode(imageNamed: "laserBlue01")
        laser.name = "laser"
        laser.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "laserBlue01"), size: laser.size)
        laser.physicsBody?.affectedByGravity = false
        laser.physicsBody?.categoryBitMask = 4
        laser.physicsBody?.collisionBitMask = 2
        laser.physicsBody?.contactTestBitMask = 1
        laser.position = CGPoint(x: self.hero.position.x, y: self.hero.position.y + 50)
        self.addChild(laser)
        // SKActionクラスを用いてアクションを作成
        let moveToTop = SKAction.moveTo(y: frame.height + 10, duration: 0.3)
        // SKActionクラスを用いてアクションを取りやめるアクションを作成
        let remove = SKAction.removeFromParent()
        // 2つのアクションを交互に呼び出すことでレーザー光線をうったり、やめたりすることが可能になる
        laser.run(SKAction.sequence([moveToTop, remove]))
    }

タッチが終了した時点でレーザー光線が発射できる」を実装したいため、touchesEndedメソッド内に記述する。

詳しい説明は、また後日記載します。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?