Monday, November 14, 2011

"Fan" type attack

Now that we have a ball to shoot, let's shoot it!

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!

Thursday, November 10, 2011

Making the ball class

And it's a start!!

I will start slowly with small parts for a shoot 'em up game. The thing that is fun with a shooter game is to try and invent cool bullet patterns. They have to look scary but still doable. There is some classic patterns that you pretty much have to use, they are basics wall of bullets over which you'll add other things.

I'll begin by showing my Ball class which is used as my simple bullet.


package  
{
import flash.display.Sprite;
import flash.events.Event;

public class Ball extends Sprite 
{
private var color:Number;
private var radius:Number;

public var vx:Number = 0;
public var vy:Number = 0;

public function Ball(radius:Number, color:Number) 
{
this.radius = radius;
this.color = color;
drawSelf();
addEventListener(Event.ENTER_FRAME, update);
}

public function update(e:Event):void
{
x += vx;
y += vy;

if (this.x < 0 - this.radius || this. x > stage.stageWidth + this.radius || this. y < 0 - this.radius || this.y > stage.stageHeight +this.radius)
{
parent.removeChild(this);
this.removeEventListener(Event.ENTER_FRAME, update);
}
}

private function drawSelf():void
{
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
}


So it's pretty straightforward, when you call a new ball, you give it a radius and a color, it will draw itself with those and then add a listener for each frame that will call the update function.

In the update function, the ball will move by the amount of its X and Y velocity ( vx and vy ) and will remove itself if it gets out of bounds (the big IF statement).

Thats a ball!

Just try this to see it


public class Main extends Sprite 
{
public var ball:Ball = new Ball(15, 0x555555);

public function Main():void 
{
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
ball.vx = 0;
ball.vy = 0;
addChild(ball);
}
}


You get your nice ball right in the middle of the stage!! BRAVO
Play with the vx and vy properties to wee it move, you can also easily change the size and color.

Ok that was easy, I know. Next time we'll build a nice "fan" pattern the flood your screen with bullet. Until then, try it yourself!!

Monday, November 7, 2011

Here we gooooo!

Hi everyone or anyone!

Here's the start of my geeking blog!

The main mission of this blog is to give my additional motivation to develop my ActionScipt 3 skills, get something done and eventually put a completed game on this internet thing. So the plan is to put some snippets of codes and little demo of things I'm doing. Right now I'm working (albeit slowly) on a Shoot 'em up so I'll probably be posting some bullet pattern and simple moving codes.

Also since I'm a game collecting geek, I'll be posting some game reviews or game related topics. In fact, the last game I got (10 minutes ago, no joke) is The Lost Vikings for the SNES and in pretty good shape. I'll give it a try soon.