Flash Game Development Series One: Enemies in the game (4)

Source: Internet
Author: User

Article Three:Flash game Development Series One: The enemy in the game .

Four (insert), half random moving enemy

Take a look at this example:

We can see that an enemy moves from top to bottom according to certain rules. The main rule is top-down, but the range of movement is random. This approach is often used in early games, including some large games. In fact, this is a very simple exercise, but if used properly, it is also a threat.

We'll probably look at the principle:

The enemy takes eight steps in one direction at random speed, then changes direction, and then walks eight steps. In this example, in order to simply show that the enemy goes to the bottom of the screen, we will let it back to its original position and go again.

We create an object enemy_obj to preserve the enemy's motion state, and of course you can give the object's attributes directly to enemy this MovieClip. We should be clear about this. Enemy_obj have some properties, let me introduce:

Speedx:x direction Speed
Speedy:y direction Speed
DirX: Direction,-1 or +1
Cur: Current number of steps

We write an initialization function init () to initialize the data:

init = function () {
Enemy_obj.sppedx = enemy_obj.sppedy=0;
Enemy_obj.dirx =-1;
enemy_obj.cur = 0;
enemy._x = 150;
Enemy._y = 50;
};

After initialization is done, that's the function of the enemy's movement.

The direction we initially defined is (-1), which is the negative direction to X. Then began to continue to increase the number of steps, Enemy_obj.cur = (++enemy_obj.cur)%8 can be a concession number to maintain within 8 steps, do not understand the Flash can trace the data to see, in fact, is very simple, is to continuously increase the number of steps, and for 8 modulo. Take the mode of use you can check the help to see, there is not much to say.

Once you reach a certain step, here is the first step, then start to change direction, through Enemy_obj.dirx = (Enemy_obj.dirx = = 1)? -1:1 to achieve. And then we start the random generation speed, where we're going to specify the enemy to go down, so the y direction is fixed, the x direction changes with the direction of the enemy_obj, so we multiply the velocity of x at random, Times Enemy_obj.dirx, and if you're a novice, now you should understand why Enemy_obj.dirx going to change between 1 and 1?

The rest may not be explained more, the following is the complete first frame source code:

var enemy_obj:object = new Object ();
init = function () {
Enemy_obj.sppedx = enemy_obj.sppedy=0;
Enemy_obj.dirx =-1;
enemy_obj.cur = 0;
enemy._x = 150;
Enemy._y = 50;
};
* Functions * *
Tracker = function () {
Enemy_obj.cur = (++enemy_obj.cur)%8;
if (enemy_obj.cur = = 1) {
Enemy_obj.dirx = (Enemy_obj.dirx = = 1)? -1:1;
Enemy_obj.speedx = (15+random (5)) *enemy_obj.dirx;
Enemy_obj.speedy = 1+random (5);
}
Enemy._x + = Enemy_obj.speedx;
Enemy._y + = Enemy_obj.speedy;
if (enemy._y>380) {
Init ();
}
Updateafterevent ();
};
/* Run it*/
Init ();
SetInterval (Tracker, 25);

The program still uses a simpler structure so that everyone can see it clearly. If you are a novice, then please be sure to understand the role and effect of these statements, I have such an old subject to take out, mainly in order to open up new ideas, if you are a master, then these things are simple.

The program is pretty ugly, right? It doesn't matter, because this tutorial is mainly about this way, the real application to your own program, is to add a lot of elements, if it can be used well, will be very effective.

I've opened a post for this series of tutorials that you can discuss, and of course, ask questions here.

  this time the source code please download here .



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.