Game Programming is VERY mathematical if you haven't fugured that out yet. Check this out, this is the movement program for my enemies in "Space Gunner":
hp = 2;
spd = 17;
pow = 5;
typ = 4;
life = true;
//get random x y values
var j = random(4) + 1;
switch (j) {
case 1 :
//left edge
x = 0;
y = random(601);
break;
case 2 :
//right edge
x = 800;
y = random(601);
break;
case 3 :
//top edge
y = 0;
x = random(801);
break;
case 4 :
//bottom edge
y = 600;
x = random(801);
break;
default :
x = 0;
y = 0;
break;
}
this._x = x;
this._y = y;
//find the angle from point to (400,300)
r = Math.atan2(x-400,y-300);
a = (180/Math.PI)*r + 90;
this._rotation = a;
//find the x y increment
xi = -Math.sin(r);
yi = -Math.cos(r);
count = 0;
function special () {
//agility
count += 1;
agility = Math.sin(count/2) * spd;
ar = Math.atan2(x-400, y-300) + (Math.PI/2);
axi = -Math.sin(ar);
ayi = -Math.cos(ar);
x += agility * axi;
y += agility * ayi;
}
function incr() {
// find the angle from point to (400,300)
r = Math.atan2(x-400, y-300);
a = (180/Math.PI)*r;
this._rotation = -a + 180;
// find the x y increment
xi = -Math.sin(r);
yi = -Math.cos(r);
if (hp<=0) {
this.gotoandplay("exp");
}
d = Math.sqrt((y-300) * (y-300)+(x-400) * (x-400)) - (this._height/2);
if (d <= 50) {
//if (this.hitTest(_root.boundary)) {
_root.hp -= pow;
this.gotoandplay("exp")
}
x += (xi*(spd*10))/18;
y += (yi*(spd*10))/18;
special();
this._x = x;
this._y = y;
}