You could easily just shoot the ball with a addChild() command and putting a number in the vy property if you only wanted a single bullet going down but to make it interesting, we need more!
We are going to make a customizable fan shaped attack pattern, a classic! It will look like that :
Now that we know what we are making, let's see the code :
public class Main extends Sprite
{
private var numberBullet:uint = 20;
private var speed:Number = 5
private var angle:Number;
private var spread:Number = 90;
private var gapWidth:Number = spread / (numberBullet - 1);
private var startingAngle:Number = 45;
//timers
private var timrFire:Timer = new Timer(500, 50);
public function Main():void
{
init();
}
private function init():void
{
timrFire.addEventListener(TimerEvent.TIMER, fire);
timrFire.start();
}
private function fire(e:TimerEvent):void
{
for (var i:Number = 0; i < numberBullet; i++)
{
var bullet:Ball = new Ball(5, 0xFF0000);
var angle:Number = startingAngle + (gapWidth*i);
var convertedAngle:Number = angle * Math.PI / 180;
bullet.x = stage.stageWidth / 2;
bullet.y = 50;
bullet.vx = speed * Math.cos(convertedAngle);
bullet.vy = speed * Math.sin(convertedAngle);
addChild(bullet);
}
}
}
}
First we set a timer to call the fire() function and set its delay to 500 milliseconds. It means fire() will be called every half of a second. The second parameter means that it'll call the function 50 times.
The heart of this is in the fire() function. We have a for statement that will add the number of bullet you just set in the numberBullet variable. Then, it gives it the angle at which the bullet will travel. You set the point in x and y you want it to start and it sets its vx and vy properties. Finally, each ball is added to the stage.
You've got four variables you can use to easily change your fan. Let's make it so we have twelve bullets moving slower in a bigger arc. The spread will be the "width" of the arc and the startingAngle is the angle at which the first bullet will start at. A zero will make it go straight to the right.
private var numberBullet:uint = 12;
private var speed:Number = 3
private var spread:Number = 180;
private var startingAngle:Number = 0;
There you have it, a nice little wall of bullets!
What pattern will come next? We'll see. In the meantime, The Legend of Zelda Skyward Sword is coming out so I'll be playing that and probably post my impressions.
Until then, have a nice day!
What pattern will come next? We'll see. In the meantime, The Legend of Zelda Skyward Sword is coming out so I'll be playing that and probably post my impressions.
Until then, have a nice day!