PDA

View Full Version : Platformer AI



AdamZZ
February 12th, 2007, 03:23 PM
ok i have tried to make AI and failed the code is not even worth posting here >.< but do you think you can help me with simple AI, the only thing i need help with is have a bunch of enemys on a map and make every enemy thats in the screen to follow me thats all.. look in the fla its a halo game (im not following any tutorials..) the physics its not the best but its my own :P except for the hittest i took it from the platfrom tut on this site :P
but if cannot get any AI thats like i want it i make it like kirby/mario and have the enemys go back and forwardd all the time xD but if thats the case i need help making the enemys attack (it would be easier if all enemys had the name Enemy (more enemys coming)). the first enemy is just a black box then im gonna put in some halo enemys :P but i need some basic code first..

i hope you understand what i want my english isnt the best :P but Thanks for all the help Kirupa rules :bu:

when making the code just make a black box and make it a mc with instance of enemy :P (uhm i think you already know that but its better if i say it :P)

Rar with .fla, .swf (http://upload.kinstry.co.uk/users/adamzz/halo%20gamo.rar)

DangerousDan
February 13th, 2007, 09:34 AM
If you want them to follow you, check the distance between you and the enemy if the distance is less than or equal to whatever then tell him to move in.


agressionLevel = 10;
speed = 5;
if(dist <= agressionLevel)
{
if(enemy._x > player._x)
{
enemy._x -= speed;
}

else if(enemy._x < player._x)
{
enemy._x += speed;
}
}

Loop through for multiple enemies.

AdamZZ
February 13th, 2007, 11:20 AM
hehe thanks xD thats just too simple 0.o.. ohh yeah >.< i saw some flaw in your code 0.o that code only works for 1 enemy >.< i need to change the code everytime i put in a new enemy and that will be annoying >.<


Edit: oh yeah saw that you said loop trough but im not good at that 0.o can you give me an exemple ? or i will look at some tut :P

Insane Knux
February 13th, 2007, 07:37 PM
Do you use
duplicateMovieClip
or
attachClip?
Cause if ya do, you need your variable that keeps count of the enemies in the code.


agressionLevel = 10;
speed = 5;
if(dist <= agressionLevel)
{
if(_root["enemy"+i]._x > player._x)
{
_root["enemy"+i]._x -= speed;
}

else if(_root["enemy"+i]._x < player._x)
{
_root["enemy"+i]._x += speed;
}
}

DangerousDan
February 14th, 2007, 06:07 PM
If a enemy or player is on a different Y, you might need to add Pythagorean theorem (find distance: a squared + b squared = c squared.)

On insane's code you'll loop through using a for loop:
ActionScript Code:

for(i=0;i <= numberOfEnemy;i++)
{
code goes here
}


Perhaps making an array of alive enemies and looping through that might work, depends on how you want to do this. Make sure the enemy is alive before you do the AI code.

AdamZZ
February 16th, 2007, 09:27 AM
uhm >.< im not understanding 0.o but i will try too do it using insane's and dan's codes.