Flash Game Development Series One: The Enemy in the Game 1

Source: Internet
Author: User
Tags abs setinterval
For the current Flash, it's not strange to develop some really big games. I am involved in the content and development of the game related, but are some of the more basic knowledge. If you are a master of game development, you can completely ignore what I have described here. My plan is to do a series of tutorials, which is one of the series.

Game is an interactive work, simply said, is through the user's behavior, will have a certain impact on the process of this work. Speaking of the game, always mention the difficulty, the difficulty of the game is: when you want to achieve a certain purpose, you will find that its implementation is somewhat difficult, the difficulty is difficult to overcome, the greater the difficulty. Different types of games have different difficulty, and different implementation methods. For example: Tetris by changing the speed of the box drop to change the difficulty of the game, air combat shooting game through different enemy and different Boss to achieve different difficulties.

In this series, we would like to work with you to study the enemy's movement in the game, 1.1 points. As the base is better to have some, otherwise there will be a little headache.

   first, the most direct tracking

First look at this example:


Assuming that the red circle is the player, the green circle is the enemy, move your mouse, and the enemy will follow you.

This is the simplest way to track an enemy, the principle of which is:

if (Player x coordinates <> enemy x coordinates) {
Adjust enemy x coordinates, approaching player x coordinates
}
if (player y-coordinate <> enemy y-coordinate) {
Adjust the enemy Y coordinates, approaching the player y-coordinate
}

This should be extremely easy to understand. So what does a specific code implementation look like?

We first put two different MovieClip on the stage, one instance name is the player, and the other is called enemy.

For convenience, we only use the mouse to achieve the player's movement, so the code is very simple:

player._x = _xmouse-10;
player._y = _ymouse-10;
Updateafterevent ();


Players can move, the following to solve the problem of coordinate adjustment.


Look at the picture above, no matter where the player and the enemy position, as long as does not coincide, two characters always have a certain distance between, we use dx and dy to represent the x direction and the y direction of the difference. According to DX and DY, based on the concept that the enemy is near the player, we can draw the direction that the enemy should move forward.

The enemy should have a certain speed, according to this speed to the player near. So we can first define a variable to represent the speed of the enemy: Enemyspeed.

According to the analysis, we can draw the following calculation formula:

DX = player._x-enemy._x;
dy = player._y-enemy._y;
if (math.abs (dx) >=enemyspeed) {
Enemy._x + = ((dx>=0) Enemyspeed:-enemyspeed);
}
if (Math.Abs (dy) >=enemyspeed) {
Enemy._y + = ((dy>=0) Enemyspeed:-enemyspeed);
}


Observed, we use a math.abs (dx) >=enemyspeed to limit the enemy's movement, in fact, can not be limited, but that the enemy at a higher speed, it will occur jitter phenomenon. Because of this situation, the enemy's coordinates and player coordinates between the difference is small, the enemy may be in the approaching process of swinging constantly. We can try to remove the restriction.

For as unfamiliar, I would like to explain this: enemy._x + = (dx>=0), Enemyspeed:-enemyspeed), in fact, is equivalent to the following sentence:

if (dx >= 0) {
enemy._x = enemy._x + enemyspeed;
} else {
enemy._x = Enemy._x-enemyspeed;
}


This is used to determine the direction of the enemy's movement, according to DX dy positive and negative conditions, to determine which direction to move.

OK, so far, I think it's all explained, here is the complete first frame source code:

var enemyspeed:number = 2;
var dx, Dy:number;
* Functions * *

Tracker = function () {
player._x = _xmouse-10;
player._y = _ymouse-10;
DX = player._x-enemy._x;
dy = player._y-enemy._y;
if (math.abs (dx) >=enemyspeed) {
Enemy._x + = ((dx>=0) Enemyspeed:-enemyspeed);
}
if (Math.Abs (dy) >=enemyspeed) {
Enemy._y + = ((dy>=0) Enemyspeed:-enemyspeed);
}
Updateafterevent ();
};
/* Run it*/
SetInterval (tracker, 10);


For as Novice: The program defines the variable, determines the speed of the enemy's motion, this can be changed, function Tracker is mainly used to deal with player movements and enemy movement. Updateafterevent is to ensure that the flow of the set, no or can.

If the tracker function is not triggered, then this program will not run, so we use the setinterval, every 10 milliseconds to trigger a tracker function, so that the program is running normally.

This time the introduction is here, very simple, isn't it? Next time we're going to add a little bit of functionality to the current enemy, or limit it.

   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.