RTL_Shadow
363 posts
|
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.
|
|
|
jonathanasdf
3835 posts
|
|
|
|
RTL_Shadow
363 posts
|
Im trying to figure out how to. Would it be easy, Im not the best programmer.
|
|
|
MCOBigBen
256 posts
|
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.
|
|
|
jonathanasdf
3835 posts
|
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.
|
|
|
RTL_Shadow
363 posts
|
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.
|
|
|
Jabor
11382 posts
|
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.
|
|
|
jonathanasdf
3835 posts
|
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.
|
|
|
Jabor
11382 posts
|
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.
|
|
|
RTL_Shadow
363 posts
|
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?
|
|
|
jonathanasdf
3835 posts
|
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.
|
|
|
iCheese
209 posts
|
//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.
}
|
|
|
jonathanasdf
3835 posts
|
that is off, because it’s only the X distance that’s calculated.
|
|
|
iCheese
209 posts
|
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.
|