If you have already made a moving character (type 2), this Tutorial will be very easy for you.

Set up your new AI enemy exactly as you did your moving character (make sure that everything in the main movie clip is CENTERED). Give it the instance name "AI2" and use the following script to control it (instead of key down detection):


onClipEvent(enterFrame){
var AI_speed = 6; //the higher this number the faster the AI moves
var reaction_distance = 300; //this higher this number, the farther the AI can "see" your character from.

var AI_width = this._xscale / 2;
var AI_hight = this._yscale / 2;

if(this._y < _parent.character._y){ var view_direction2 = "down"; var y_dif = _parent.character._y - this._y; this._xscale = 100; if(y_dif > x_dif){
gotoAndStop("down");
}
} else {
var view_direction2 = "up";
var y_dif = this._y - _parent.character._y;
this._xscale = 100;
if(y_dif > x_dif){
gotoAndStop("up");
}
}

if(this._x < _parent.character._x){ var view_direction = "right"; var x_dif = _parent.character._x - this._x; this._xscale = 100; if(x_dif > y_dif){
gotoAndStop("right");
}
} else {
var view_direction = "left";
var x_dif = this._x - _parent.character._x;
this._xscale = -100;
if(x_dif > y_dif){
gotoAndStop("left");
}
}

if(view_direction == "left"){
if(x_dif > AI_width + 100){
this._x -= AI_speed;
} else {
if(y_dif < AI_hight + 1){ trace("you've been hit!"); } } } else if(view_direction == "right"){ if(x_dif > AI_width){
this._x += AI_speed;
} else {
if(y_dif < AI_hight){ trace("you've been hit!"); } } } if(view_direction2 == "up"){ if(y_dif > AI_hight){
this._y -= AI_speed;
} else {
if(x_dif < AI_width + 1){ trace("you've been hit!"); } } } else if(view_direction2 == "down"){ if(y_dif > AI_hight){
this._y += AI_speed;
} else {
if(x_dif < AI_width + 1){ trace("you've been hit!"); } } } }

Be Sociable, Share!