computergeek67
|
Artificial InteligenceArtificial intelligence means that one object will follow another. To make A.I. happen, first you need to make an object and convert it into a Movie Clip. Instance name it "player" without the quotes and paste this code:
| Code: | onClipEvent(load){spd=15;}
onClipEvent(enterFrame) {
_x-=Key.isDown(37)*spd;
_x+=Key.isDown(39)*spd;
_y-=Key.isDown(38)*spd;
_y+=Key.isDown(40)*spd;
} |
That makes the Movie Clip move with the arrow keys.
Next make another Movie Clip. Open the actions panel and paste this code:
| Code: | onClipEvent(load){
spd=10; //Set enemy speed
}
onClipEvent(enterFrame){
//Rotate enemy to face player
Xdiff=_parent.player._x-_x;
Ydiff=_parent.player._y-_y;
radAngle=Math.atan2(Ydiff,Xdiff);
_rotation=int((radAngle*360/(2*Math.PI))+90);
updateAfterEvent();
if(this.hitTest(_parent.player)){
//Do attack
}else{
//Move
if(_rotation>180){
_y+=(spd*Math.cos(Math.PI/180*_rotation));
_x-=(spd*Math.sin(Math.PI/180*_rotation));
}else{
_y-=(spd*Math.cos(Math.PI/180*_rotation));
_x+=(spd*Math.sin(Math.PI/180*_rotation));
}
}
} |
If done right, when you move your "player" Movie Clip, the enemy should follow its every move.
|