Game Development Series 1: enemies in the game (1) http://www.flashempire.com/school/tutorview.php? The id530 is not a strange thing for the current Flash to develop games like a decent one. The content I mentioned here is related to game development, but it is basically Syntax.
Game Development Series 1: enemies in the game (1)
Http://www.flashempire.com/school/tutorview.php? Id = 530
For the current Flash, it is not a strange thing to develop a game that looks like a decent one. The content I mentioned here is related to game development, but it is a basic knowledge. If you are an expert in game development, you can ignore the content I described here. My plan is to do a series of tutorials, one of which.
A game is an interactive work. Simply put, it affects the process of the work through user behavior. When it comes to games, you always need to mention the difficulty. the difficulty of a game is: when you want to achieve a certain purpose, you will find it difficult to implement. the more difficult it is to overcome, the more difficult it will be. Different types of games have different difficulty and different implementation methods. For example, Tetris changes the difficulty of the game by changing the speed at which the blocks fall. air combat shooting games use different enemy planes and different Boss to achieve different difficulties.
In this series, we want to work with you to explore the movements of enemies in the game. It is best to have some AS basics, otherwise it will be a little headache.
1. most direct tracking
First, let's look at this example:
Assume that the red circle is a player and the Green Circle is an enemy. Move your mouse and the enemy will follow you.
This is the simplest way to track enemies. The principle is: if (Player x coordinates <> enemy x coordinates) {adjust the enemy x coordinates, 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 should the specific code implementation look like?
We first place two different MovieClip on the stage. One instance is called player and the other is called enemy.
For convenience, we only use the mouse to move the player, so the code is very simple: player. _ x = _ xmouse-10; player. _ y = _ ymouse-10; updateAfterEvent ();
Players can move, and the following will solve the problem of coordinate adjustment.
Looking at the picture above, no matter where the player and the enemy are, as long as they do not overlap, there is always a certain distance between the two roles, we use dx and dy to represent the difference between x and y. According to dx and dy, based on the concept that the enemy is close to the player, we can draw the direction that the enemy should move forward.
The enemy should have a certain speed, which is used to bring the enemy closer to the player. So we can first define a variable to represent the enemy's speed: enemySpeed.
According to the analysis, we can obtain 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 );}
We used a Math. abs (dx)> = enemySpeed can be used to limit the movement of the enemy. However, when the speed of the enemy is relatively high, jitter may occur. In this case, the difference between the enemy's coordinates and the player's coordinates is small, and the enemy may keep swinging during the approaching process. You can remove the restrictions and try again.
If you are not familiar with AS, let me explain this sentence: 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 movement of the enemy. the direction of movement is determined based on the positive and negative conditions of dx dy.
Okay, so far, I have explained it all clearly. below is the complete first frame of 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 beginners of AS: the program first defines variables and determines the speed of the enemy's movement. this can be changed. the Function tracker is mainly used to process player movement and enemy movement. UpdateAfterEvent is set to ensure smoothness.
If the tracker function is not triggered, the program will not run. Therefore, we use setInterval to trigger the tracker function every 10 milliseconds. in this way, the program runs normally.
This is the introduction here. it's easy, isn't it? Next time, we will add some small functions to the current enemy, or restrict it.
Please download the source code here.