Subscribe to this Blog

Actionscript 3.0 Basics: Handling Events

By admin • Aug 6th, 2009 • Category: Tutorials


Okay, so now let’s follow up from where we left off. A couple of weeks ago I wrote about working with classes. Now I’ll move on to one of the very important parts of actionscript, ie. handling events. An event is basically any activity that can cause flash to respond by calling some part of code into action.

Now, I’ll continue to work with classes and use something called an “addEventListener” into the code to handle events. While working on this, if you do not follow the exact steps that I’ve done(or follow some other steps that work ;) ) you will come across various errors. Since I came across these errors while working on this tutorial I’ll be posting a section at the end of this tutorial that will cover the three most frequent errors that I got while making this and how to get rid of them.

Okay, for starters we need a .fla file, I’ve named mine as “MovingObject.fla”. Inside this flash file we need to create a single movie clip called “hero” with the following attributes:

movieClip

Now, we will use this flash file along with a class file “MovingObject.as”. For those of you who are not familiar with how to work on class files, you can check out the tutorial here. Do not forget to link your .as file to your .fla file.

Since we are using a .as file we’ll need to write some code into it. So here goes:

  1. package {
  2.     import flash.display.*;
  3.     import flash.events.*;
  4.     public class MovingObject extends MovieClip {
  5.         var hero1:hero = new hero();
  6.         var speed:int= 5;
  7.         public function MovingObject() {
  8.             hero1.x=275;
  9.             hero1.y=200;
  10.             addChild(hero1);
  11.             addEventListener(Event.ENTER_FRAME, moveHero);
  12.         }
  13.         function moveHero(event:Event) {
  14.             hero1.x+=speed;
  15.             if(hero1.x>=525 || hero1.x<=25)
  16.             {
  17.                 speed=-speed;
  18.             }
  19.         }
  20.     }
  21. }

Alrighty, now as usual here’s the explanation of the above code. Pay attention:

LINE 1: It declares that this Actionscript file contains a package that defines a class.
LINE 2: Imports a “Library Class” flash.display.* (ie. All functions in flash.display)[If you don’t know what a library class is just google it as usual.
LINE 3: Imports another library class for handling events.
LINE 4: Defines that this class is public ie. It can be accessed by the MovieClip. The name of the class is MovingObject (note: the file must also be saved as MovingObject.as). the “extends MovieClip” part is appended so as to tell flash that this class works in unison with a MovieClip (the root is also a movieclip)
LINE5: Declares a new movieclip “hero1” of type hero
LINE6: Declares a variable speed which has the scope in all functions within the class.
LINE7: Defines the constructor function of the class.
LINE8-9: Specify initial position for “hero1”.
LINE 10: Puts the newly created movieclip in its appropriate position
LINE 11: The prodigal event listener is here: This line basically asks flash to wait for an “On Enter Frame” event, ie. Basically its triggered ‘n’ times a second if your flash movie runs at ‘n’ fps. On being triggered its asked to call a function “moveHero”.
LINE13: Definition of moveHero function.
LINE14-17: Simple logic to movie the hero1 move from end to end of the flash stage.

If you’ve done everything properly, after you save your .as file and compile the .fla file you should see something like this:

Now lets talk about the common errors you might face:

Error 1120: Access of undefined property: You’ll usually get this problem if you are using the hero1.x=123 or such type of statements outside of functions inside a class. Make sure all such statements are inside the class.

Error 1046: Type was not found or was not a compile time constant. I got this error for a long time for the word “event” then I finally realized that I had not imported the flash.events class.

Error 1180: Call to possibly undefined method: This error was given for the “addEventListener” statement when it was used outside of a function. This and other such statements need to be used inside of a function.

If you wish you can download the source files for this tutorial here. I hope you enjoyed it, more are on the way.

If you liked this post, Buy me some beer (or coffee)

Tagged as: , , , , , ,

admin is An engineering student from India. Enjoying his life both in the real world and in cyberspace
Email this author | All posts by admin

One Response »

  1. [...] that you can create a class(shown here) and handle events(as shown here). It’s time for some user interaction. Now in this tutorial I’ll be showing how to make a [...]

Leave a Reply