AS: Main. the source of good, evil and actionscripting
AS: Artificial thinking
related topics:
AS: Random
AS: Conditions/loops
what’s this about?
this is about getting your mind into the computer’s mind. Making it think like you think, act stupid but not too stupid. All that crap to make give your game that edge above the competition.
getting started
every time that I’m about to code an AI there is one thing that I think of. The AI does not have a fast reaction time. so the odds of it attacking when it get’s the chance aren’t 100%. So to start of we’ll set up a variable to see whether it will attack.
onClipEvent(enterFrame){
a = int(Math.random()*5)
if(a==4){
//we’ll add what it does in here
}
}
ok. What that does is set up ‘a’ to be a random integer (whole number) every frame and if a = 4 that it will attack.
Now, we don’t want our AI to be a complete dunce and attack when you’re nowhere near it. So we’ll have to set up a variable to calculate the distance between the two objects.
onClipEvent(enterFrame){
distance=_root.enemy._x-_root.player._x
}
that will measure the enemys _x and compare it with the player’s _x to calculate how far apart they are.
but what are we supposed to do with this information?
Well, now that we’ve got the ability to check the distance between the object’s and the ability to make it attack at a random time. we’ll combine them together.
onClipEvent(enterFrame){
distance=_root.enemy._x-_root.player._x
if(distance<100 && distance>-100){
a = int(Math.random()*5)
if(a==4){
_root.enemy.gotoAndStop(attackFrame)
}
}
}
that’s all well and good but how do I make it follow?
as I am writing this I am making a game kind of like unreal flash. So I am assuming that that is the type of game you are coding. Now for this type of game the AI is going to need to get power ups and everything. So it won’t always just hunt down the player.
So what I did is make a movieclip containing all the main point’s in the level and the player’s _x. the timeline was like this:
1----- -----2----- -----3----- ----4---- -----5----- ----6----- ----7------ ----8----- -----9
point-----point------point-- ---point-----point-----point--- ---point------point---player
on every point frame I put the actions:
_root.POINTX=_x+_root.point._x
_root.POINTY=_y+_root.point._y
and then on the player’s one I had:
_root.POINTX=_root.player._x
_root.POINTY=_root.player._y
Now, this makes the POINTX and POINTY variables equal to what the point would be on the point’s movieclip.
Go onto the main timeline and click on the point’s movieclip. Give it the instance name of “points” and give it these actions:
onClipEvent(load){
_visible=false
gotoAndStop(int(Math.random()*_totalframes
)+1)
}
All that does is make the point’s movieclip invisible and make it go to a random frame.
Now back to the enemy movieclip. We want to make it so that he will go looking for the _root.POINTX and _root.POINTY. getting him to the _root.POINTY is not as important as the POINTX and it’s much harder to incorporate.
We’ll start off by making him run after POINTX:
onClipEvent(enterFrame){
if(_root.POINTX>this._x){
_x+=6
} else if(_root.POINTX<this._x){
_x-=6
}
if(this._x-3>_root.POINTX && this._x+3<_root.POINTX){
_root.points.gotoAndStop(int(Math.random()
*_root.points._totalframes)+1)
}
}
now to explain the code:
if(_root.POINTX>this._x){
_x+=6
} else if(_root.POINTX<this._x){
_x-=6
}
this make’s the enemy run after the pointX depending on whether it’s above it or below it.
if(this._x-3>_root.POINTX && this._x+3<_root.POINTX){
_root.points.gotoAndStop(int(Math.random()
*_root.points._totalframes)+1)
}
this part is a little more complex. It checks’ whether the pointX is greater than the enemy’s _x-3 and below the enemy’s _x+3 so that the enemy won’t have to be on the exact x co-ordinates as the point for the point to go to a random frame.
what about the point Y?
well for the point Y we want it so that if it’s smaller than the enemy’s _y that he will jump. But we don’t want him to continually jump like crazy.
So we’ll set up a variable:
j=int(Math.random()*10)
then we’ll make it so that he will jump:
if(j==6){
//thing’s to do when he jumps
}
now combining that part:
onClipEvent(enterFrame){
j=int(Math.random()*10)
if(j==6){
//jump
}
}
now to incorporate the Y point into it:
onClipEvent(enterFrame){
if(_root.POINTY<this._y){
j=int(Math.random()*10)
if(j==6){
//jump
}
}
}
That simply check’s if the Point Y is below the enemy’s _y that j will be a random integer and if it is 6 that it will jump.
now what do I do?
well you are so close to finishing your artificial intelligence but I’m going to leave you to trial/error your way to customising it. I hope that you learnt something and that you can soon get your brain into an artificial intelligence. Thankyou and goodnight.