4
4

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を試してみる11 pathを使ってホーミング

Posted at

iphonePlay_20150608.gif

敵がうねうね動いてプレイヤーに向かってきたり、敵の弾がこちらに向かって来るようにして難易度を上げてみる。

黄緑の敵は出現したタイミングでのプレイヤーの位置を目指して、うねうね動いて接近してくる

if (enemy.enemyColor == SKColor.greenColor()) {

	// プレイヤーの位置
	let dragon: DragonUnit = childNodeWithName("dragon") as! DragonUnit
	// CGPath の宣言
	let path:CGMutablePath = CGPathCreateMutable()
	let y = Int(arc4random() % 450) + 150
	// 始点を決定
	CGPathMoveToPoint(path, nil, CGRectGetMaxX(self.frame), CGFloat(y))
            
	var cp1x : CGFloat = (2 * CGRectGetMaxX(self.frame) + dragon.position.x)/3
	var cp1y : CGFloat = CGRectGetMinY(self.frame)
	var cp2x : CGFloat = (CGRectGetMaxX(self.frame) + 2 * dragon.position.x)/3
	var cp2y : CGFloat = CGRectGetMaxY(self.frame)
                        
	//最終目標はプレイヤー
	CGPathAddCurveToPoint(path, nil, cp1x, cp1y, cp2x, cp2y, dragon.position.x, 	dragon.position.y)

	//プレイヤーの位置から画面の端までの動き                     
	cp1x = dragon.position.x - (CGRectGetMaxX(self.frame) - dragon.position.x)/3
	cp1y = CGRectGetMinY(self.frame)
	cp2x = dragon.position.x - (CGRectGetMaxX(self.frame) - dragon.position.x)*2/3
	cp2y = CGRectGetMaxY(self.frame)
            
	CGPathAddCurveToPoint(path, nil, cp1x, cp1y, cp2x, cp2y, 	CGRectGetMinX(self.frame), CGRectGetMidY(self.frame))
            
	//作ったCGPathをSKActionにセット
	let sequenceLeft = SKAction.followPath(path, asOffset: false,orientToPath: false, duration: 3.0)
	let remove = SKAction.removeFromParent()
	let sequence = SKAction.sequence([sequenceLeft, remove])
           
	// 敵SKSpriteNodeのrunActionにセット 
	enemy.runAction(sequence)
}

黄色の敵の場合は弾をプレイヤー目指して撃ってくる

var enemyShot1Last:CFTimeInterval! // 敵ショットタイミング

override func update(currentTime: CFTimeInterval) {
    
  //敵ショット
     if(enemyShot1Last == nil){
         enemyShot1Last = currentTime
     }
        
     if(enemyShot1Last+1.5 <= currentTime){
         if(!self.paused){
             enemyShot1Fire()
         }
         enemyShot1Last = currentTime
     }
}

敵の弾は黄色の敵が画面上にある場合のみ出力する。

if (enemy.enemyColor == SKColor.yellowColor()){
	enemy.name = "yellow"
}

敵の弾はプレイヤーを目指しつつ、画面の外にまで向かう必要があるので出発点からプレイヤーまでのx軸とy軸の距離と、画面の端(x軸=0)から端時点のでy軸を求める

if((childNodeWithName("yellow")) != nil){
	let action = SKAction.animateWithTextures([b1, b2, b3, b4], timePerFrame: 0.5)
	fire.runAction(SKAction.repeatActionForever(action))
	fire.size = CGSizeMake(60, 60)
            
	fire.position.x = enemyYellow.position.x + 5
	fire.position.y = enemyYellow.position.y
            
	let dragon: DragonUnit = childNodeWithName("dragon") as! DragonUnit
            
	let path:CGMutablePath = CGPathCreateMutable()
    
    // yの座標算出
	let y2 : CGFloat = fire.position.y -
	 (fire.position.x * (fire.position.y - 	dragon.position.y))/
	 (fire.position.x - dragon.position.x)
            
	CGPathMoveToPoint(path, nil, fire.position.x, fire.position.y)
	CGPathAddLineToPoint(path, nil, 0, y2)
	
	fire.runAction(sequence)
            
    self.addChild(fire)
}
4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?