{Web Space Tutorials}

good tutorials, made easy

Object Oriented Programming, Actionscript 3.

Introduction

Welcome to the first Actionscript 3 tutorial. Today we will be learning about objects in AS3 along with how to create and manipulate them. If you're completely new to AS3 but familiar with previous versions of AS or even Javascript this is a good starting point for you. Otherwise you may find the information a bit overwhelming.

Prerequisites

For this tutorial it is recommended that you have:


How To Create An Object

The first thing you must learn about AS3 is how to make an object. First create something on your stage, then convert it into a button or movieclip and provide it with an instance name. The same can be done with a text field as well. Next create the object via actionscript using variables. Here are some examples for you:

  1. var someNumber:Number = 17.239;
  2. var someNegativeInteger:int = -53;
  3. var someUint:uint = 22;
  4. var firstName:String = "George";
  5. var soliloquy:String = "To be or not to be, that is the question...";
  6. var niceWeather:Boolean = true;
  7. var playingOutside:Boolean = false;
  8. var seasons:Array = ["spring", "summer", "autumn", "winter"];
  9. var employee:XML = <employee>
  10. <firstName>Harold</firstName>
  11. <lastName>Webster</lastName>
  12. </employee>;

Controlling Properties of an Object

Now you will learn how to manipulate the properties of an object and actually bring it to life. The syntax for this is relatively simple,

  1. Square.x=100;
  2. Square.rotation=-80;
  3. Square.rotation=triangle.rotation;

The common structure: you use a variable or instance name (square, triangle, rectangle, circle) as the name of the object, followed by a period (.) and then the name of the property (x, y, rotation, scaleX, scaleY, width, height, alpha, rotation, visible).


Object Methods

Now that you know about object properties, it is time to learn about object methods. Methods are simply the action that an object performs.

  1. Square.play();
  2. Square.stop();
  3. Square.gotoAndPlay(3); or Square.gotoAndPlay(“here”);

Methods, like properties, are accessed by writing the object’s name (a variable), then a period, and then the name of the method followed by parentheses. The parentheses are the way that you indicate that you are calling the method, or in other words, instructing the object to perform that action.

Sometimes values (or variables) are placed in the parentheses, as a way to pass along additional information that is necessary to carry out the action. These values are known as method parameters.

For example, the gotoAndStop() method needs information about which frame to go to, so it requires a single parameter in the parentheses. Other methods, like play() and stop(), are self-explanatory, so they don’t require extra information. Nevertheless, they are still written with parentheses.

Basic Event Handling

Last for this lesson are event handling. Event handling is a fancy way of saying, the technique for specifying certain actions to perform in response to an event or set of events. When you are writing ActionScript code to perform event handling, there are three important elements you’ll want to identify:

This may sound a bit overwhelming at first, so here is an example to help clarify:

  1. square.play();

what this code means is that when the play_btn button is clicked, the playNow function will be executed.


Now you try

Now that you've learned all about AS3 objects it's time you tried an exercise yourself. A great way to practice objects is a simple gotoAndStop function. I want you to open a new flash document and using your new knowledge make a frame animation that plays until frame 5 then stops. Then have a button that when clicked jumps to frame 15 and stops. It would help to have some sort of object or picture on frames 5 and 15, so it is easy to tell if your button works.


Quiz

Now that you've read the tutorial, take the quiz.