How to Call a Function in AS3

Functions are used almost for every AS3 (ActionScript 3.0) Flash project. They are used to draw graphics, play sounds, and send text messages. Most importantly though, they are used to reduce the amount of AS3 code that you must write. AS3 functions are sections of code that can repeatedly accessed with a function call statement. This means that if you want to draw a circle 10 times with AS3 code, instead of writing the code to draw a circle 10 times you would just write the code once and then write code to call the function ten times instead.

Things You'll Need

  • Adobe Flash Professional
Show More

Instructions

    • 1

      Start the Flash program. Click "New" from the "File" menu on the main Flash menu bar. Click the file type "Flash File(ActionScript 3.0) from the "New Document" dialog box that appears. Click the "OK" button on the lower right hand side of this dialog box to close the dialog box.

    • 2

      Select the "Actions" option from the "Window" menu on the main Flash menu bar to open the "Actions" editor. Position your mouse cursor on the first line of the Action's editor. Click your mouse button. Type the code below on the first line of the Actions editor to call a function named "myFunction;"

      myFunction();

      Press the "ENTER" key on your keyboard to move the mouse cursor to line 2 of the Actions editor.

    • 3

      Type the the function code for "myFunction" listed below on lines two and three on the Action's editor to make the AS3 program display the words "my first function call" when the flash movie is played.

      function myFunction(): void

      {trace("my first function call");}

    • 4

      Click the "TestMovie" option in the "Control" menu on the main Flash menu bar. Read the statement you traced, "my first function call" that appears in the "OUTPUT" display panel.

    • 5

      Replace the f AS3 code you typed in above with the code below in the Actions editor to call the "myFunction" function ten times and display the "my first function call" message 10 times using the "for" loop AS3 statement. Copy and paste the code below directly into the Actions editor to save time.

      for (var i:int = 1; i <10; i++)

      {myFunction(); }

      function myFunction(): void

      {trace("my first function call");}

    • 6

      Click the "TestMovie" option in the "Control" menu on the main Flash menu bar. Observe the statement you traced, "my first function call" is repeated 10 times in the "OUTPUT" display panel.

Learnify Hub © www.0685.com All Rights Reserved