14
13

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を試してみる4 流れる背景と動くキャラ

Last updated at Posted at 2015-03-01

iphonePlay_2015021501.gif

ショットを口から吐いてる感じにもしてみました。

背景はグラデーション画像メーカーで作りました。ここで3000×650で作った画像にペイントソフトで適当に山っぽいものを追加。
画像のつなぎを考慮して端の方には何も描かない。

backView2.jpg

この画像を背景として、ある程度右から左にゆっくり動かした後、ある座標に一瞬で移動するというのを永遠に実行させる。

    override func didMoveToView(view: SKView) {
        //背景
        let backView = SKSpriteNode(imageNamed: "backView2.jpg")
        backView.position = CGPointMake(self.size.width, 420)
        backView.runAction(SKAction.repeatActionForever(
            SKAction.sequence([
                SKAction.moveToX(-430.0, duration: 13.0),
                SKAction.moveToX(self.size.width+480, duration: 0.0)])))
        self.addChild(backView)

作った画像をどう動かすかは手探りで適当な数値セットしてます。

キャラも適当にペイントソフトで竜をイメージした何かを描いて(まだ謙遜が足りないでしょうか)、竜以外を透明にする(プレビュー.app使いました。簡単に出来て良いよね)。

        //キャラ画像
        let texture = SKTexture(imageNamed: "charImage1.png")
        let dragon = SKSpriteNode(texture: texture)
        dragon.name = "dragon"
        dragon.position = CGPoint(x: 80, y: CGRectGetMidY(self.frame))
        
        self.addChild(dragon)

SKTextureを使ってますが、キャラに動きを出さない限りあまり意味がないのかな。

それでキャラはタップした位置に移動し、その後に火を出して貰いたいのでtouchesBeganを修正

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */
        
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            
            let dragon: SKSpriteNode = childNodeWithName("dragon") as SKSpriteNode
            //let dragon = SKNode().childNodeWithName("dragon")
            let x = pow(location.x - dragon.position.x, 2)
            let y = pow(location.y - dragon.position.y, 2)
            let z = sqrt(x + y)/250.0
            let moveToTouch = SKAction.moveTo(location, duration: NSTimeInterval(0.5 * z))
            dragon.runAction(moveToTouch, completion: {
                
            let firePath = NSBundle.mainBundle().pathForResource("MyParticle", ofType: "sks")
            
            let fire = NSKeyedUnarchiver.unarchiveObjectWithFile(firePath!) as SKEmitterNode
          
            fire.particleSize = CGSize(width: 70, height: 70)
            fire.position.x = location.x + 100
            fire.position.y = location.y
            //衝突イベントに必須
            fire.physicsBody = SKPhysicsBody(rectangleOfSize: fire.particleSize)
            fire.physicsBody?.contactTestBitMask = 0x1 << 0
            fire.physicsBody?.categoryBitMask = 0x1 << 1
            fire.physicsBody?.mass = 20         //重量 衝突時のダメージの大きさを表現出来る

         
            let follow = SKAction.moveByX(-1000, y: -350, duration: 3.5)
            let follow2 = SKAction.moveByX(1000, y: 0, duration: 3.5)
            let remove = SKAction.removeFromParent()
            let sequence = SKAction.sequence([follow2, remove])
            
            fire.particleAction = follow
            fire.runAction(sequence)
            
            self.addChild(fire)
         })
        }
    }

今までのショットを出すロジックを、キャラの移動後に実行するようにしてます。
後、ショットを出しっぱなしにしてたのを、移動後に消すように変更。残しておくとだんだん動きが重くなってしまいますしね。
一応これで意図した動きにはなりますが、素早くタップするとキャラの位置とショットの出所がずれてしまいますね。そこを制御するか、するならどうするか、という課題が残っています。

02/16 なんかdragonのrunAction2回実行しているという変なロジックあったので修正。ついでに他の文章もちょい修正。

14
13
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
14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?