Game development Series A game of enemies 1_php tutorial

Source: Internet
Author: User
Game Development Series One: Enemies in the game (1)
http://www.flashempire.com/school/tutorview.php?id=530
For the current Flash, the development of some smarty pants game is not a strange thing. I am involved in the content and development of the game, but are some of the more basic knowledge. If you are an expert in game development, you can completely ignore what I have described here. My plan is to do a series of tutorials, which is one of them.
The game is an interactive work, simply said, is through the user's behavior, will have a certain impact on the process of the work. When it comes to the game, the difficulty is always mentioned: when you want to achieve a certain purpose, you will find that its implementation is somewhat difficult, the more difficult the difficulty to overcome, the greater the difficulty. Different types of games have different difficulty, and different implementation methods. For example, Tetris changes the difficulty of the game by changing the speed of the falling of the block, and the air combat shooting game can achieve different difficulties by different enemy planes and different bosses.
In this series, we want to work with you to study the game of the enemy's movement, 1.1 points. As the base is better to have some, otherwise it will be a little headache.
First, the most direct tracking
First look at this example:
Assume that the red circle is the player, the green circle is the enemy, move your mouse, the enemy will follow you run.
This is the simplest tracking enemy, its principle is: if (Player X coordinate <> enemy x coordinate) {Adjust the enemy x-coordinate, approaching player x-coordinate} if (Player y-coordinate <> enemy y-coordinate) {Adjust enemy y-coordinate, approaching player y-coordinate}
This should be extremely easy to understand. So what is the specific code implementation supposed to look like?
We first put two different MovieClip on the stage, one instance named player and the other 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 ();
The player can move, the following to solve the problem of coordinate adjustment.

Look at the picture above, regardless of the player and the enemy position, as long as not coincident, two characters always have a certain distance, we use dx and dy to represent the x direction and the y direction difference. According to DX and DY, based on the concept of the enemy approaching the player, we can draw the direction that the enemy should go forward.
The enemy should be at a certain speed, approaching the player according to this speed. So we can first define a variable to represent the speed of the enemy: Enemyspeed.
According to the analysis, we can draw the following 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 used a math.abs (dx) >=enemyspeed to limit the enemy movement, in fact, can not limit, but that in the enemy at a higher speed, there will be jitter phenomenon. Because of this situation, the enemy's coordinates and the player's coordinates are small, and the enemy may keep swinging during the approximation process. You can remove the limit and try it out.
For as less familiar, I would like to explain this: enemy._x + = ((dx>=0) Enemyspeed:-enemyspeed), is actually 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 the positive and negative conditions of DX dy, to decide which direction to move.
OK, so far, I think it's all clear, 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 first defines the variable, determines the enemy's movement speed, this can be changed, the function tracker is mainly used to deal with the player movement and enemy movement. Updateafterevent is to ensure the smoothness of the set, no also can.
If the tracker function is not triggered, then the program does not run, so we use setinterval to trigger the tracker function every 10 milliseconds, so the program runs normally.
This time the introduction is here, very simple is not 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.

http://www.bkjia.com/PHPjc/531979.html www.bkjia.com true http://www.bkjia.com/PHPjc/531979.html techarticle Game Development Series One: Enemies in the game (1) http://www.flashempire.com/school/tutorview.php?id=530 for the current Flash, the development of some smarty pants game is not what odd ...

  • 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.