シューティングゲームを作る4


主な更新点
画面内の弾の管理をArrayからVector
n-way弾を発射する敵機クラス実装
自機狙いn-way弾を発射する敵機クラス実装


n-way弾


自機狙いn-way弾


n-way弾発射敵機クラス

package
{
	import flash.display.Sprite;
	//N-way弾発射敵機の基本クラス
	public class BE_Nway extends BE_VarySpeedAndRotation
	{
		//発射角度範囲
		public var shot_angle_range:Number;
		
		public function BE_Nway(sp:Sprite, bullet_color:uint, shot_speed:Number, shot_angle:Number, shot_angle_range:Number, 
			shot_angle_speed:Number, interval:Number, shot_count:uint, bullet_type:uint, bullet_angle_rate:Number, bullet_speed_rate:Number,
			 bullet_height:Number, bullet_width:Number, 
			hp:uint, speed:Number, hit_area:Number, speed_rate:Number=0, angle:Number=0, angle_speed:Number=0)
		{
			this.shot_angle_range = shot_angle_range;
			this.shot_count = shot_count;
			//描写
			graphics.beginFill(0x00FF00);
			graphics.drawCircle(x, y, 10);
			super(sp, bullet_color, shot_speed, shot_angle, shot_angle_speed, shot_count, interval, bullet_type,
				bullet_angle_rate, bullet_speed_rate, bullet_height, bullet_width, hp, speed, hit_area, speed_rate, angle, angle_speed);
		}
		
		override public function shot():void{
			var ball:Bullet;
			
			if(hp > 0){
				if(shot_count > 1){
					//発射角度を中心として
					//発射角度範囲の広さに
					//発射数の弾を撃つ
					for(var i:uint = 0; i < shot_count; i++){
						ball = new Bullet(bullet_type,bullet_height, bullet_width, bullet_color, shot_angle+shot_angle_range*(i/(shot_count-1))-shot_angle_range/2, 
							bullet_angle_rate, shot_speed, bullet_speed_rate, sp, bullet_width/2);
						ball.target = target;
						Shooting3.ary.push(ball);
						sp.addChild(ball);
						//発射する角度を変える
						ball.x = x;
						ball.y = y;
					}
				}
				else{
					//弾生成
					ball = new Bullet(bullet_type,bullet_height, bullet_width, bullet_color, shot_angle, bullet_angle_rate, shot_speed, 
					bullet_speed_rate, sp, bullet_width/2);
					ball.target = target;
					Shooting3.ary.push(ball);
					sp.addChild(ball);
					//発射する角度を変える
					ball.x = x;
					ball.y = y;
				}
				//発射する角度を変える
				shot_angle += shot_angle_speed;
			}
		}
	}
}


発射する範囲を角度で指定してその範囲内に発射。
角度の計算部分
shot_angle+shot_angle_range*(i/(shot_count-1))-shot_angle_range/2



自機狙いn-way弾敵機クラス

package
{
	import flash.display.Sprite;

	public class BE_aim_Nway extends BE_Nway
	{
		public function BE_aim_Nway(sp:Sprite, bullet_color:uint, shot_speed:Number, shot_angle_range:Number, interval:Number, shot_count:uint, bullet_type:uint, bullet_angle_rate:Number, bullet_speed_rate:Number, bullet_height:Number, bullet_width:Number, hp:uint, speed:Number, hit_area:Number, speed_rate:Number=0, angle:Number=0, angle_speed:Number=0)
		{
			super(sp, bullet_color, shot_speed, 0, shot_angle_range, 0, interval, shot_count, bullet_type, bullet_angle_rate, bullet_speed_rate, bullet_height, bullet_width, hp, speed, hit_area, speed_rate, angle, angle_speed);
		}
		
		override public function shot():void{
			var ball:Bullet;
			
			if(hp > 0){
				if(shot_count > 1){
					//発射角度を中心として
					//発射角度範囲の広さに
					//発射数の弾を撃つ
					for(var i:uint = 0; i < shot_count; i++){
						ball = new Bullet(bullet_type,bullet_height, bullet_width, bullet_color, shot_angle+shot_angle_range*(i/(shot_count-1))-shot_angle_range/2, 
							bullet_angle_rate, shot_speed, bullet_speed_rate, sp, bullet_width/2);
						ball.target = target;
						Shooting3.ary.push(ball);
						sp.addChild(ball);
						//発射する角度を変える
						ball.x = x;
						ball.y = y;
					}
				}
				else{
					//弾生成
					ball = new Bullet(bullet_type,bullet_height, bullet_width, bullet_color, shot_angle, bullet_angle_rate, shot_speed, 
					bullet_speed_rate, sp, bullet_width/2);
					ball.target = target;
					Shooting3.ary.push(ball);
					sp.addChild(ball);
					//発射する角度を変える
					ball.x = x;
					ball.y = y;
				}
				//発射する角度を変える
				
				shot_angle = -Math.atan2(target.y - y, target.x - x)/Math.PI*180+90;
				trace(shot_angle);
			}
		}
		
	}
}


自機へ向かって弾を発射するために自機への発射角度を求める


shot_angle = -Math.atan2(target.y - y, target.x - x)/Math.PI*180+90;

target.x, target.y: 自機の現在座標
x, y: 敵機の現在座標


atan2メソッドを使うと結構簡単に求めることができます。


ソースダウンロード