Learn how to make your enemy move and to follow the player around the stage using basic artificial intelligence. It might be useful to follow my player movement tutorial before embarking on thisone.
Step One
Create a new movieclip and name its instance 'enemy'. Inside the movieclip, on a new layer, we are going to add a walk function. Call this function 'walkTo' and give it the variables X and Y. We will add the code within this function in a couple of steps.
function walkTo(X, Y) {
}
scooters
Step Two
Now, before we begin writing the 'walkTo' function it might be helpful if we add a call to it so we can see what we are doing as changes are being made. On the main timeline, on the enemy instance add this code.
onClipEvent (enterFrame) {this.walkTo(_root.player._x, _root.player._y);}
This will call our walkTo function and also give it the coordinates of the player movieclip too.
Step Three
We are now ready to begin writing our walkTo script. Within the walkTo function, let's first add a speed variable.
Next, lets deal with vertical movement on the Y-axis. If say, the enemy had a co-ordinate of 300 and the player was at 100, the enemy would have to reduce its Y co-ordinate in order equal that of the player's. We can do this by subtracting our speed variable. Here's how we'll do it.function walkTo(X, Y) {var speed = 3;
}
function walkTo(X, Y) {
var speed = 3;
// Y-Axis movement
if (this._y>(Y)) {
this._y -= speed;
}
}
function walkTo(X, Y) {
var speed = 3;
// Y-Axis movement
if (this._y>(Y)) {
this._y -= speed;
} else if (this._y<(Y)) {
this._y += speed;
}
}Once done, you will see that the enemy can now move either up or down. But hold on, there seems to be some jittering going on when the enemy has equalled the player's Y co-ordinate. This is because the two if statements are flip-flopping between each other. We need to create a margin so that both statements can be happy. Do this simply by adding and subtracting a little from each of the statements targets. This means that the movieclip can find a nice resting spot between the two points.
// Y-Axis movement
if (this._y>(Y+20)) {this._y -= speed;} else if (this._y<(Y-20)) { this._y += speed;
}
Step Four
Now that we have our vertical movement sorted out, let add some horizontal. It works in exactly same way. You'll notice that also have new line which alters the _xscale. This makes sure the movieclip faces left when walking left and faces right when walking right.
function walkTo(X, Y) {
var speed = 3;// Y-Axis movement
if (this._y<(Y-20)) {this._y += speed;} else if (this._y>(Y+20)) {
this._y -= speed;}// X-Axis Movement
if (this._x<(X-40)) {this._x += speed;this._xscale = -100;} else if (this._x>(X+40)) {
this._x -= speed;
this._xscale = 100;
}
}
That's good. Could also try this:
ReplyDeleteonEnterFrame = function () {
dis = Math.sqrt(Math.pow(ball._x-_xmouse, 2)+Math.pow(ball._y-_ymouse, 2));
if (dis>40) {
angle = Math.atan2(ball._y-_ymouse, ball._x-_xmouse);
angle = Math.floor(angle/(Math.PI/4))*(Math.PI/4);
ball._x -= Math.cos(angle)*2;
ball._y -= Math.sin(angle)*2;
}
};
Just a little more fun :P
Works great too. Thanks!
ReplyDeleteHey Guys!
ReplyDeleteWhen tho source will be available?
Hey guys, how do I make the ennemy run away from my char?
ReplyDeleteI love how they show these garden variety tutorials but they don't have one for the Flash Fight game.
ReplyDeleteTo make the enemy run away reverse the math - if you are adding, subtract, if you're subtracting, add.
ReplyDeleteI love this :)
ReplyDeleteIt is also in the event that you are here searching free of charge uproar focuses then you are newriotpoints.com
ReplyDeleteMinecraft Premium Account Generator. free minecraft accounts
ReplyDeleteIs it true that you are searching free of charge steam codes? free steam codes
ReplyDeleteItunes blessing card codes, itunes blessing card generator. free itunes code
ReplyDeleteFree Minecraft Premium Account Generator permits you simply that play minecraft free online
ReplyDeleteIn the event that you see this message when recovering a code spotify gift card
ReplyDeleteWe will help you on the best way to get free psn card codes,
ReplyDeleteNeed a free PSN Code Generator?
how to get free psn codes
Nintendo Wii diversions at CD Universe, Great administration, secure requesting and quick. free wii points
ReplyDeleteDownload New Itunes Codes Generator 2015 Free Working Here:
ReplyDeletehttp://dlhack.com/download/itunes-code-generators
http://dlhack.com/download/itunes-code-generators
http://dlhack.com/download/itunes-code-generators
http://dlhack.com/download/itunes-code-generators
http://dlhack.com/download/itunes-code-generators
http://dlhack.com/download/itunes-code-generators
http://dlhack.com/download/itunes-code-generators
http://dlhack.com/download/itunes-code-generators
http://dlhack.com/download/itunes-code-generators
http://dlhack.com/download/itunes-code-generators
http://dlhack.com/download/itunes-code-generators
http://dlhack.com/download/itunes-code-generators
For Download: Click Here HackersContent.com
ReplyDeleteFor Download: Click Here HackersContent.com
For Download: Click Here HackersContent.com
For Download: Click Here HackersContent.com
For Download: Click Here HackersContent.com
Very informative blog post.Really looking forward to read more. Keep writing...
ReplyDeletelol elo
mmorpg oyunlar
ReplyDeleteinstagram takipçi satın al
Tiktok jeton hilesi
tiktok jeton hilesi
antalya saç ekimi
referans kimliği nedir
instagram takipçi satın al
Mt2 Pvp Serverler
instagram takipçi satın al
perde modelleri
ReplyDeletemobil onay
mobil odeme bozdurma
nft nasil alınır
ANKARA EVDEN EVE NAKLİYAT
trafik sigortası
DEDEKTOR
Site kurma
ASK KİTAPLARİ