Welcome to Virtual Tutorial World Guest!



Artificial Inteligence

 
Post new topic   Reply to topic    VTW Forum Index -> Actionscript
View previous topic :: View next topic  
Author Message
Please Register and Login to this forum to stop seeing this advertsing.





Tokens:


Posted:     Post subject:

Back to top
computergeek67
Administrator
Administrator


Joined: 31 Jul 2007
Posts: 144


Tokens: 147
Location: US

PostPosted: Mon Oct 08, 2007 9:10 pm    Post subject: Artificial Inteligence Reply with quote

Artificial 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.



_________________



Come to me if you have any questions
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    VTW Forum Index -> Actionscript All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Card File  Gallery  Forum Archive
Powered by phpBB © 2001, 2005 phpBB Group
Virtual Tutorial World ©2007-2008 computergeek67

Affiliates