When you are creating a basic game, there are several different code structures that you will be using. There are three main parts of any AS3 code:  		 Variables -  This is how your data is stored. Data can be numbers, words (strings), objects, and more. Variables are defined by the code var and must be one word.   var playerHealth:Number = 100;  // "var" designates that you are defining a variable. // "playerHealth" is the variable name. // "Number" is the type of data. // "100" is the value assigned to the variable. // All actionscript lines end with ";"    Event Handlers - Event handlers look for specific things to occur, and then tells the rest of the program. This is essential for player input and repeating code. Event handlers typically call upon functions.   addEventListener(MouseEvent.CLICK, swingSword);  // "addEventListener()" defines the event handler. // "MouseEvent" is the category of input that is being listened for. // ".CLICK" is the specific event in the MouseEvent category. // "swingSword" is the function that is called when the event occurs.    Function - Sections of code assigned to a keyword that can be called upon later. Functions handle the bulk of your game's programming, and complex games can have hundreds of functions while simpler games may only have a few. They can be in any order since they only work when they are called upon.   function swingSword (e:MouseEvent):void; { 	//Your code goes here }  // "function" is the keyword that appears at the start of every function. // "swingSword" is the name of the function. // "e:MouseEvent" is an added parameter, showing that the function  // is called from the event listener. // ":void" is the value that is returned by the function. If no value // is returned, use :void. ActionScript is used to affect objects in Flash. In order to make a game, you will need to create objects that the player will interact with. Depending on the guides you are reading, objects may be referred to as sprites, actors, or movie clips. For this simple game, you will be creating a rectangle.  		 Open Flash Professional if you haven't already. Create a new ActionScript 3 project. Click the Rectangle drawing tool from the Tools panel. This panel may be in different locations depending on the configuration of Flash Professional. Draw a rectangle in your Scene window. Select the rectangle using the Selection tool. With your newly-created rectangle selected, open the Modify menu and select "Convert to Symbol". You can also press F8 as a shortcut. In the "Convert to Symbol" window, give the object an easily recognizable name, such as "enemy".  		 Find the Properties window. At the top of the window, there will be a blank text field labeled "Instance name" when you hover over it. Name it the same as you did when you converted it to a symbol ("enemy"). This creates a unique name that can be interacted with through AS3 code. Each "instance" is a separate object that can be affected by code. You can copy the already created instance multiple times by clicking the Library tab and dragging the instance onto the scene. Each time you add one, the name will be changed to designate that it's a separate object ("enemy", "enemy1", "enemy2", etc.). When you refer to the objects in the code, you simply need to use the instance name, in this case "enemy". Once you have an instance made, you can adjust the properties through AS3. This can let you move the object around the screen, resize it, and so on. You can adjust properties by typing the instance, followed by a period ".", followed by the property, followed by the value:  		  enemy.x = 150; This affects the position of the enemy object on the X-axis.  enemy.y = 150; This affects the position of the enemy object on the Y-axis. The Y-axis is calculated from the top of the scene.  enemy.rotation = 45; Rotates the enemy object 45° clockwise.  enemy.scaleX = 3; Stretches the width of the enemy object by a factor of 3. A (-) number will flip the object.  enemy.scaleY = 0.5; Squishes the object to half its height. This command will return the current values for specific objects, and is useful for determining if everything is running as it should. You may not want to include the Trace command in your final code, but it is useful for debugging. Now that you have a basic understanding of the core functions, you can create a game where the enemy changes size every time you click on it, until it runs out of health.  var enemyHP:Number = 100;  // sets the enemy's HP (health) to 100 at the start. var playerAttack:Number = 10;  // sets the players attack power when they click.   enemy.addEventListener(MouseEvent.CLICK, attackEnemy);  // By adding this function directly to the enemy object,  // the function only happens when the object itself is  // clicked, as opposed to clicking anywhere on the screen.   setEnemyLocation();  // This calls the following function to place the enemy // on the screen. This occurs when the game starts.   function setEnemyLocation ():void {  enemy.x = 200;   // moves the enemy to 200 pixels from the left of the screen  enemy.y = 150;   // moves the enemy down 150 pixels from the top of the screen  enemy.rotation = 45;   // rotates the enemy 45 degrees clockwise  trace("enemy's x-value is", enemy.x, "and enemy's y-value is", enemy.y);   // Displays the current position of the enemy for debugging }   function attackEnemy (e:MouseEvent):void  // This creates the attack function for when the enemy is clicked {  enemyHP = enemyHP - playerAttack;   // Subtracts the attack value from the HP value,   // resulting in the new HP value.  enemy.scaleX = enemyHP / 100;   // Changes the width based on the new HP value.  // It is divided by 100 to turn it into a decimal.  enemy.scaleY = enemyHP / 100;   // Changes the height based on the new HP value    trace("The enemy has", enemyHP, "HP left");   //Output how much HP the enemy has left } Once you've created the code, you can test your new game. Click the Control menu and select Test Movie. Your game will begin, and you can click the enemy object to change its size. Your Trace outputs will be displayed in the Output window.
تعرف على وحدات البناء الأساسية لكود أكشن سكربت 3. أنشئ كائن (Object). عيِّن الخصائص المميزة للكائن. تعلم كيفية تغيير خصائص المثيل. اختبر أمر التتبع trace(). اصنع لعبة بواسطة المعلومات سابقة الذكر. جرب اللعبة.