Now: Tutorial for Web and Software Design > Flash > Action Script > Flash Content
> Action Scripting [Bookmark it]
Action Scripting

ActionScriptingTaking the First Step

I am planning to write a series of tutorials to help with ActionScript. In this first part, I will introduce ActionScripting and try to make clear where to write your code and the scope of your scripts. We will speak about accessing objects at the scene using ActionScript. If any point is unclear for you, please write to me and I will add a comment for this tutorial to make everything more understandable.

I think you know what is Flash, how to draw or write something on the scene and how to convert it into a symbol. ActionScript is a programming language which has a syntax very smilar to EcmaScript [or JavaScript] and is the heart of the Flash. By using ActionScript, your Flash movies will become "applications." You will be able to add interactivity to your movies.

ActionScript is an easy-to-learn object oriented programming language. It's object oriented but you don't need to declare a class or have any knowledge on objects. You will be able to use built-in objects at the end of this tutorial. Being the first step, we will learn where to write your code in Flash.

In Flash, you can write your code 'attached' to keyframes or symbols instances . Select a keyframe or an instance of a symbol at the scene then go to the ActionScript editor. [From now on, you are an 'expert' according to the ActionScript editor and you need to use Expert Mode of the editor.] What you select [a symbol instance or a keyframe] is what your code is attached to.

A code that is attached to a keyframe is executed when that frame plays (ie the playhead passes it).

If you write your code into [I mean attached to] a keyframe, you can access the symbol instances by using their names only. Lets look at this example:

  • Create a new flash document. The only and first frame will be selected.
  • Select the type tool and from the text field property drop-down box in the properties panel, select the option 'dynamic'.
  • Click somewhere on the scene to create a dynamic text "object". You may type something into it.
  • Name it by typing into the instance name box in the properties panel. I use myfirsttext as the name.
  • Now, select the keyframe containing the text object.
  • In the ActionScript Editor, write this code:

    myfirsttext.text="Helloooo Woooorld!!!";
  • Now ctrl+enter or Control>>Test Movie to see the result.

We named something on the scene, we accessed it and changed a property of it. text property is a property of the textfield object; it carries the inner text of the text box.

Before looking at the second place to write the code into (ie attaching code to symbol instances instead of keyframes) lets take a closer look at objects referencing to them.

The "dot" Syntax

We used a dot (.) in our first example, between the object (myfirsttext) and it's property (text). The dot syntax is very common but I will explain it because I sayed you don't need to know anything about objects.

An object in a programming language is like an object in real life. Take your mouse as an example. Lets name it mymouse. Now we can access it's right_button by writing mymouse.right_button. This right button can be clicked by the "method" mymouse.right_button.click(). When we use this method, an event will occur: mymouse.right_button.onClick. Getting this idea will be enough: the leftside of the dot is the owner of the rightside of the dot.

Absolute Referencing

In ActionScript, the master owner of everything in a movie is _root. If you create a symbol instance on the scene, and name it theobject, you can access it from anywhere in the movie typing _root.theobject. If you have the symbol instance (myinstance) on the scene at level 0 (it means the symbol is owned by directly the movie not by another symbol) and in that symbol, you have another (mychildinstance), you can call it _root.myinstance.mychildinstance This is an absolute reference for the object that works from everywhere in the movie.

Relative Referencing

If we want to attach code to a keyframe of mychildinstance object, we can access myinstance object with the keyword: _parent. This keyword makes possible for us to access the owner of where this code is written. We can access another object on the scene named myfirsttext from mychildinstance by using _root.myfirsttext or _parent._parent.myfirsttext. The first _parent is myinstance and the second one means the parent of myinstance, ie _root. You should understand this very well in order to avoid first-step-errors writing your ActionScript code.

"this" Keyword

this keyword is used to reference the object's itself. For now, it is enough to learn this. We will use it in function implementations but you don't need to worry about it now.

Coding into Symbol Instances

You can also write code attached to symbol instances. The code attached to instances consists of event handlers for that symbol instance. If symbol is a button, the instance will be a button object and if it is a movie clip the instance will be a movieClip object. You can use on(name_of_the_event) or onClipEvent(name_of_the_event) event handlers. For example, create a rectangle on the stage and convert it into a movie clip symbol by pressing F8. Select it and write this code into the editor:

on (rollOver) {
trace("I am an ActionScript expert");
}

Control>>Test Movie and move your mouse over the rectangle. You will see our message in the output window. When you type on ( and wait, a scrollable box will appear that contains the available event names. You may chose from the list or also write it by hand. The code between { } braces will be executed when the event occurs. trace() is a builtin ActionScript function which doesn't have any meanings for the final product. It is used only when authoring to see what the code is doing to find out errors. rollOver is the name of the event when occurs you move your mouse over the object.

The difference between on() and onClipEvent() is the types of the events they handles. on() is for button events. I didn't say for button's, be careful; you can use them also with movieClip objects. These events are standart events for buttons, like onRollOver or onRelease (occurs when you click the instance).

Movie clip events are used for only with movieClip objects. For example, one of them is load which occurs when a movie clip loads (ie it is downloaded completely)

Lastly, summary of what we have learned from this tutorial:

  • We can attach ActionScript code to keyframes or symbol instances.
  • The code attached to a keyframe runs when that keyframe is played.
  • The code attached to a symbol instance consists of event handlers
    and runs when that events occur.
  • Every object is owned by _root.
  • Every object has a _parent which is the owner.
  • What comes after a dot (.) is owned by what is before the dot.
    Example: world.eurasia.turkiye.istanbul

Don't hesitate to comment or write to me. I am waiting for your participation.


[Bookmark][Print] [Close][To Top]
  • Prev Article-Flash:

  • Next Article-Flash:
  • Related Materias
    Motion Animations
    Shape Transformations
    Effects on Animations
    Generating and Publishing 
    Introduction to ActionScri
    Examples of ActionScript 2
    Handwriting Flash Movie
    Butterfly Wings Animation
    Working with Objects
    Attaching Movie Clips usin
    Topics
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Graphic Design Tutorial
     

    Coreldraw Tutorial

      Illustrator Tutorial
      3D Graphics Articles
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial&Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial&Articles
     

    XML Style Tutorial

      AJAX Tutorial
      XML Mobile
    Flash Tutorial&Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial&Articles
     

    Linux Tutorial

      Symbian Tutorial
      MacOS Tutorial