Artificial Intelligence

Subscribe to Artificial Intelligence 14 posts

Sign in to reply


 
avatar for RTL_Shadow RTL_Shadow 363 posts
Flag Post

No tutorial I can find has this. The shootorial just tells it to appear in random places. I dont want that. Would it be hard for me to program MC_enemy1 to chase me within a range of 400 pixels, and if he is less than that he starts runnning away? I would also like him to shoot. If this is possible, how many lines of code could it take?

P.S. I am using AS2. Not 3.

 
avatar for jonathanasdf jonathanasdf 3835 posts
Flag Post

try it yourself.

 
avatar for RTL_Shadow RTL_Shadow 363 posts
Flag Post

Im trying to figure out how to. Would it be easy, Im not the best programmer.

 
avatar for MCOBigBen MCOBigBen 256 posts
Flag Post
Originally posted by RTL_Shadow:

Im trying to figure out how to. Would it be easy, Im not the best programmer.

Think about it in normal English first. Write it out. Break it down into simple parts before trying to figure out what the code would be. Like:

enemy position – my position = difference
If the difference is greater than 400, chase me

Chase me means: Figure out where I am on the screen, move towards me.

The whole thing at once sounds complicated, and you’re not going to find a tutorial that covers everything you want to do together, but once you break it down it gets easier. I’m sure you can find a tutorial on calculating differences between object positions, even if you couldn’t find a tutorial on making it chase you.

 
avatar for jonathanasdf jonathanasdf 3835 posts
Flag Post

In your code, you can already spawn it at random places, and move it forward. Now, think about how you would get it to move towards the player. Maybe with some geometry? On each frame, get the X and Y positions of the player, and the ship’s own X and Y positions. From that you can use pythagoras to find the total distance from the ship to the player, as well as the X distance and Y distance. Then, say you want the ship to move at a speed of 5 pixels/frame. The amount to move the ship in the x axis would be (5 * X distance / total distance) and the amount to move the ship in the y axis would be (5 * Y distance / total distance).

Try to put that in first and make the ship move towards the player. After that think about the things about the distances and behaviours.

Edit: Lol I have the steps in a different order than ben.

 
avatar for RTL_Shadow RTL_Shadow 363 posts
Flag Post

Is there any code that would be like this for flash?

Agro range = 400 pix
If range = 400+ (attack)
If rane = 400- (run)
If I ever made a programming language, it would be simple like that, only If’s and’s, and objects.

 
avatar for Jabor Jabor 11382 posts
Flag Post

There sure is.

var AGGRO_RANGE = 400;

function DecideWhatToDo() {
    var distance = getDistanceToPlayer();
    if(distance > AGGRO_RANGE)
        Attack();
    else
        Run();
}

All you need to do is fill in what the methods do.

 
avatar for jonathanasdf jonathanasdf 3835 posts
Flag Post

only real problem with that is that the enemy will be stuck at the 400px distance, always, and would look quite weird if its speed is high. It will keep shaking back and forth.

And if it wasn’t an enemy that shot bullets, that kind of ai would be kinda useless. I personally would also add in a bit of “hovering” motion each frame, so it looks more alive.

 
avatar for Jabor Jabor 11382 posts
Flag Post

See, that’s why I didn’t make it an onEnterFrame event. The way I planned on it being implemented is it doesn’t decide what to do every frame – it either attacks (firing one bullet, I presume) or runs away for a little bit, and then calls DecideWhatToDo() again when it’s done. No jittering, and the expected result if the player is chasing the enemy is that it runs away, turns and fires a shot, starts running again, and so on.

 
avatar for RTL_Shadow RTL_Shadow 363 posts
Flag Post

That works I guess, is there a random function that I can add that he will get a distance between 300 and 400? So it isnt just following you?

 
avatar for jonathanasdf jonathanasdf 3835 posts
Flag Post

no, you have to write the functions getDistanceToPlayer, Attack, and Run yourself.

And if it pleases you you can set AGGRO_RANGE as a const instead. lol.

 
avatar for iCheese iCheese 209 posts
Flag Post

//Assuming you only want it to attack when it's in front
//Syntax might be off
function getDistanceToPlayer(){
   return this.x - this.(parent as MovieClip).ship.x //Assumes the game is side scrolling, and the ship is in the same
   //parent movieclip. 
}

 
avatar for jonathanasdf jonathanasdf 3835 posts
Flag Post

that is off, because it’s only the X distance that’s calculated.

 
avatar for iCheese iCheese 209 posts
Flag Post

I was assuming he was making a side scroller, and you could just give the function an argument and return the x or y according to that.

Sign in to reply