Home php教程 php手册 游戏开发系列一游戏中的敌人1

游戏开发系列一游戏中的敌人1

Jun 13, 2016 am 10:26 AM
http s one develop enemy game series

游戏开发系列一:游戏中的敌人(1)
http://www.flashempire.com/school/tutorview.php?id=530
对于目前的 Flash 来说,开发一些像模像样的游戏并不是什么奇怪的事情了。我这里所涉及到的内容和开发游戏有关,但是都是一些比较基本的知识。如果您是游戏开发方面的高手,那么完全可以忽略我这里所描述的内容。我的计划是要做一系列的教程,这是其中一个系列。
  游戏是一个交互作品,简单说,就是通过用户的行为,会对这个作品的进程产生一定的影响。说到游戏,总要提到难度,游戏的难度在于:当你想要达到某种目的的时候,你会发现它的实现多少有些困难,困难越难克服,难度也就越大。不同类型的游戏有不同的难度,以及不同的实现方法。比如:俄罗斯方块通过改变方块下落的速度来改变游戏的难度,空战射击游戏通过不同的敌机以及不同的 Boss 来实现不同的难度。
  在这个系列里面,我们想和大家一起研究一下游戏中敌人的运动方式,一点一点来。AS 基础最好是有一些,不然会稍微有些头疼。
  一、最直接的跟踪
  首先看看这个例子:
  假定红色圆圈是玩家,绿色圆圈是敌人,移动你的鼠标,敌人就会跟着你跑。
  这就是最简单的跟踪敌人,它的原理是:如果(玩家x坐标 敌人x坐标){ 调整敌人x坐标,趋近玩家x坐标}如果(玩家y坐标 敌人y坐标){ 调整敌人y坐标,趋近玩家y坐标}
  这应该是极其容易理解的。那么具体的代码实现应该是什么样子的呢?
  我们先把两个不同的 MovieClip 放置在舞台上,一个实例名叫做 player,另外一个叫做 enemy。
  为了方便,我们仅仅通过鼠标来实现玩家的移动,于是代码就很简单了:player._x = _xmouse-10;player._y = _ymouse-10;updateAfterEvent();
  玩家可以移动了,下面来解决坐标调整的问题。


  看上面的图片,不管玩家和敌人处于什么位置,只要不重合,两个角色之间总是具有一定距离的,我们用 dx 和 dy 来代表 x 方向和 y 方向的差值。根据 dx 和 dy,基于敌人靠近玩家的概念,我们就可以得出敌人应该前进的方向。
  敌人应该具有一定的速度,根据这个速度向玩家靠近。所以我们可以先定义一个变量来代表敌人的速度:enemySpeed。
  根据分析,我们可以得出下面的计算公式: 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); }
  观察到,我们用了一个 Math.abs(dx)>=enemySpeed 来限制敌人运动,其实也可以不限制,但是那样在敌人速度比较高的时候,就会发生抖动现象。因为这种情况下,敌人的坐标和玩家坐标之间差值小,敌人可能会在逼近过程中不断摇摆。大家可以去掉限制尝试一下。
  对于 AS 不太熟悉的,我来解释一下这句: enemy._x += ((dx>=0) ? enemySpeed : -enemySpeed),其实是相当于下面这句: if (dx >= 0){ enemy._x = enemy._x + enemySpeed; } else { enemy._x = enemy._x - enemySpeed; }
  这是用来判断敌人的运动方向的,根据 dx dy 的正负情况,来决定向哪个方向运动。
  好,到此为止,我想已经都解释清楚了,下面是完整的第一帧源代码: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);
  针对 AS 新手:程序先定义了变量,确定了敌人的运动速度,这个是可以更改的,函数 tracker 则主要用来处理玩家移动以及敌人移动。 updateAfterEvent 是为了保证流畅度设定的,没有也可以。
  如果不触发 tracker 函数,那么这个程序是不会跑起来的,所以,我们使用了 setInterval,每隔 10 毫秒触发一次 tracker 函数,这样,程序就正常运行了。
  这一次的介绍就到这里了,很简单是不是?下一次我们要给目前的这个敌人增加一些小功能,或者说,限制一下它。
  这次的源代码请在这里下载。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Nvgpucomp64.dll causes Windows PC games to crash; Nvgpucomp64.dll causes Windows PC games to crash; Mar 26, 2024 am 08:20 AM

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

Introduction to how to download and install the superpeople game Introduction to how to download and install the superpeople game Mar 30, 2024 pm 04:01 PM

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

Four recommended AI-assisted programming tools Four recommended AI-assisted programming tools Apr 22, 2024 pm 05:34 PM

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

ASUS releases BIOS update to improve gaming stability on Intel's 13th/14th generation processors ASUS releases BIOS update to improve gaming stability on Intel's 13th/14th generation processors Apr 20, 2024 pm 05:01 PM

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.

Paving the way for PS5 Pro, the 'No Man's Sky' update code 'surprised' the game console development code name 'Trinity' and image quality configuration file Paving the way for PS5 Pro, the 'No Man's Sky' update code 'surprised' the game console development code name 'Trinity' and image quality configuration file Jul 22, 2024 pm 01:10 PM

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

Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Apr 07, 2024 am 09:10 AM

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

Learn how to develop mobile applications using Go language Learn how to develop mobile applications using Go language Mar 28, 2024 pm 10:00 PM

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

PlayerUnknown's Battlegrounds FPS optimization settings, Chicken PUBG game frame rate optimization PlayerUnknown's Battlegrounds FPS optimization settings, Chicken PUBG game frame rate optimization Jun 19, 2024 am 10:35 AM

Optimize the frame rate of the PlayerUnknown's Battlegrounds game to improve the smoothness and performance of the game. Method: Update the graphics card driver: Make sure you have the latest graphics card driver installed on your computer. This helps optimize game performance and fix possible compatibility issues. Lower game settings: Adjust the graphics settings in the game to a lower level, such as reducing resolution, reducing special effects and shadows, etc. This takes the load off your computer and increases your frame rate. Close unnecessary background programs: While the game is running, close other unnecessary background programs and processes to free up system resources and improve game performance. Clear hard drive space: Make sure your hard drive has enough free space. Delete unnecessary files and programs, clean temporary files and Recycle Bin, etc. Turn off vertical sync (V-Sync): in game

See all articles