Game Development Series 1 Enemies in the Game 1_PHP Tutorial
Game Development Series 1: Enemies in the Game (1)
http://www.flashempire.com/school/tutorview.php?id=530
For the current Flash, develop some decent ones The game isn't that weird anymore. The content I cover here is related to game development, but it is all relatively basic knowledge. If you are an expert in game development, you can completely ignore what I describe here. My plan is to do a series of tutorials, and this is one of them.
The game is an interactive work. To put it simply, the user’s behavior will have a certain impact on the progress of the work. When it comes to games, difficulty is always mentioned. The difficulty of the game is: when you want to achieve a certain goal, you will find that it is somewhat difficult to achieve. The harder the difficulty is to overcome, the greater the difficulty. Different types of games have different levels of difficulty, as well as different methods of implementation. For example: Tetris changes the difficulty of the game by changing the speed of the falling blocks, and air combat shooting games achieve different difficulties through different enemy aircraft and different bosses.
In this series, we would like to study with you the movement of enemies in the game, bit by bit. It is best to have some basic knowledge of AS, otherwise you will have a little headache.
1. The most direct tracking
First look at this example:
Assume that the red circle is the player and the green circle is the enemy. Move your mouse and the enemy will follow you.
This is the simplest tracking of enemies. Its principle is: if (player x coordinate enemy x coordinate) { adjust enemy x coordinate to approach player x coordinate} if (player y coordinate enemy y coordinates) {Adjust the enemy's y coordinate to approach the player's y coordinate}
This should be extremely easy to understand. So what should the specific code implementation look like?
We first place two different MovieClips 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();
The player can move , let’s 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 characters. We use dx and dy to represent the difference between the x direction and the y direction. According to dx and dy, based on the concept of the enemy being close to the player, we can derive the direction in which the enemy should move.
The enemy should have a certain speed and approach the player based on this speed. So we can first define a variable to represent the enemy's speed: enemySpeed.
Based on the analysis, we can derive 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); }
It is observed that we use Math.abs(dx)>=enemySpeed to limit the enemy's movement. In fact, it does not need to be limited, but when the enemy's speed is relatively high, jitter will occur. Because in this case, the difference between the enemy's coordinates and the player's coordinates is small, the enemy may continue to swing during the approach. You can try it with the restrictions removed.
For those who are not familiar with AS, let me explain this sentence: enemy._x += ((dx>=0) ? enemySpeed: -enemySpeed), which 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 enemy’s movement direction, which is determined based on the positive and negative conditions of dx dy Which direction to move.
Okay, so far, I think everything has been explained clearly. Here is the complete source code of the first frame: 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 newbies: The program first defines variables to determine the enemy’s movement speed. This can be changed. Function The tracker is mainly used to handle player movement and enemy movement. updateAfterEvent is set to ensure smoothness, and it is fine without it.
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 will run normally.
That’s it for this introduction. It’s very simple, isn’t it? Next time we will add some small functions to the current enemy, or limit it.
Please download the source code here.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



If Nvgpucomp64.dll is causing your game to crash frequently, the solutions provided here may help you. This problem is usually caused by outdated or corrupted graphics card drivers, corrupted game files, etc. Fixing these issues can help you deal with game crashes. The Nvgpucomp64.dll file is associated with NVIDIA graphics cards. When this file crashes, your game will crash too. This usually happens in games like LordsoftheFallen, LiesofP, RocketLeague, and ApexLegends. Nvgpucomp64.dll crashes games on Windows PC if N

The superpeople game can be downloaded through the steam client. The size of this game is about 28G. It usually takes one and a half hours to download and install. Here is a specific download and installation tutorial for you! New method to apply for global closed testing 1) Search for "SUPERPEOPLE" in the Steam store (steam client download) 2) Click "Request access to SUPERPEOPLE closed testing" at the bottom of the "SUPERPEOPLE" store page 3) After clicking the request access button, The "SUPERPEOPLECBT" game can be confirmed in the Steam library 4) Click the install button in "SUPERPEOPLECBT" and download

According to news from this site on April 20, ASUS recently released a BIOS update, which improves instability such as crashes when running games on Intel's 13th/14th generation processors. This site previously reported that players reported problems including that when running the PC demo version of Bandai Namco's fighting game "Tekken 8", even if the computer has sufficient memory and video memory, the system will crash and prompt an error message indicating insufficient memory. Similar crashing issues have also appeared in many games such as "Battlefield 2042", "Remnant 2", "Fortnite", "Lord of the Fallen", "Hogwarts Legacy" and "The Finals". RAD published a long article in February this year, explaining that the game crash problem is a combination of BIOS settings, high clock frequency and high power consumption of Intel processors.

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

According to news from this site on July 22, foreign media twistedvoxel discovered the rumored PS5 development codename "Trinity" and related image quality configuration files in the latest "World Part 1" update code of "No Man's Sky", which proves that Sony is expected to The PS5Pro model was recently launched. Although "No Man's Sky" has enhanced the graphics performance of the game in recent updates, many players still believe that this may be HelloGames paving the way for new models in advance. According to the latest graphics presets, under PS5 Pro The game's dynamic resolution scaling has been increased from 0.6 to 0.8, which means the game has a higher average resolution and some graphical details are upgraded from "High" to "Ultra" levels, but since each game

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

1. Right-click on a blank space on the taskbar, then find Properties and click on it. 2. Here you can see that the system default setting is not to automatically hide the taskbar. 3. Click to check, then OK to save changes. 4. When we return to our desktop, we can see that the taskbar is automatically hidden. If we move the mouse cursor to the bottom, it will be displayed again.
