Flash:AS2 Artificial Intelligence Tutorial
From GMpedia.org Wiki
Tutorial Information
The following tutorial will cover the basics and some advanced material on the topic of Artificial Intelligence. I would like to emphasize that you read every part of this tutorial since it will help you better understand this topic. AI is not an easy concept to grasp for beginners and may be hard for people new to programming. This is a tutorial that is meant for advanced programmers who have had previous expierience in making games with actionscript 2.0. The basics that you need to know for this tutorial include:
1. How to move an object on the Screen
2. Basic Drawing with flash (Note: You don't have to be awesome in drawing, just exceptional)
3. Basic tweening and animation skills
4. Advanced - Average ActionScript programmer
Basics
Artificial Intelligence, in a bag is not that complicated. Sure it can be a pain to understand at first but many people get the hang of it once they begin to explore the scripts and understand what the script is doing. The definition of ai is basically an ability that is given to the computer by a programmer which allows it to 'think for itself.'
How AI Works?
AI is nothing more that a whole bunch of 'if/else' structures combined together (aka nested if/else structures)*. Each if structure tests several conditions which determine what the computer will do. These structures are mandatory because without these 'if/else' structures, AI wouldn't work.
- This description of artificial intelligence refers to the Finite State Machine (FSM) model of artificial intelligence that game programmers often use to model intelligent behavior due to its simplicity of conceptualization and implementation.
Understanding AI
In order for a person to understand how AI works, they need to think about a problem in a very logical perspective. It is essential for the programmer to be able to think logically in order to ensure that he/she can find the best solution that there is to the problem being faced.
Lesson 01: Triangulation, Distance Formulas & AI Examples Scripts
- NOTE: All of the scripts were completed and tested in ActionScript 2.0.
Lesson 01: Triangulation is used in order to find the angle at which another object is located on screen. The image shows an object in relation to another at the three corner of the right angle triangle. Try to imagine this triangle with your creation.
Here is a simple triangulation code:
//look at image to understand the code
function triangulate(){
//Find the co-ordinates of the player and the enemy
playerx = _root.main_player._x;
playery = _root.main_player._y;
enemyx = _root.enemy._x;
enemyy = _root.enemy._y;
//Finds angle with given variables (uses Math methods)
angle = Math.atan2(playerx-enemyx, playery-enemyy)/(Math.PI/180);
//Rotates enemy in the direction of the Player. This can be used in any game for a gun which rotates and shoots at the player once he passes the restricted area.
enemy._rotation = angle;
}
this.onEnterFrame = function(){
//Initializes Function
triangulate();
}
Exercise
Create a game in which you are the player. Also create a camera that follows you while you sidescroll from one side of the screen to another. The camera will be at the center of the screen and will rotate when you make a move. Please try this exercise first before moving on with the rest of the tutorial.
--Trim9001 04:15, 18 August 2006 (CEST)Jack Soul
- This is all that has been completed at the moment. Check later for any updates that will be added to this lesson


