ActionScript Programming/PartI/Chapter 1/Page2

From Wikibooks, open books for an open world
Jump to: navigation, search

Properties[edit]

XMouse, YMouse[edit]

Say you have a button and the user clicks it. You want to get the position of the mouse. This property will help you get the coordinates of the mouse.

Step 1

Create any button (by drawing it yourself or taking it from Common Libraries) and set its name to "button1".

Step 2

Select the Text Tool Mfas image006.jpg from the Toolbox and draw it. Then select it and in the Properties window set its width to 100, height to 20, font size to 20, Var to "info" and change the Text Type to "Dynamic Text". There are 3 types of Text Type

  • Static Text – you choose this type of Text if you don’t want the value of the Text to be changed. For example, if you just want to "hello" in the Text and you don’t want "hello" to be changed during runtime, then you use "Static Text". This is good for usual texts.
  • Dynamic Text – this type of Text can be change at any time during runtime. This type of Text is very useful if you want to write information of current process.
  • Input Text – this is a text field and is almost the same as "Dynamic Text". The only difference between these two types is that "Input Text" Text can be changed by the user, but "Dynamic Text" can’t be changed at all.

So we use "Dynamic Text" type in our example to see the values of xmouse and ymouse. "Var" property is value of the Text. For example if you have a "Input Text", which’s "Var" property is set to "zzz" and want to retrieve its value, then you can get it from the "zzz". I mean that the value of the textbox is stored in "zzz". But by using the "Var", in our case "zzz", you can also change the value of the Text". For example if you write

zzz = "hello";

then the value of the Text will become "hello";

Step 3

Right click the button you have created and choose "Actions" from the popup menu to open the actions of the button. Make sure that the "Advanced Mode" is turn on and write the following:

on (release) {
   info = "x: " + button1._xmouse + "; y: " + button1._ymouse;
}

The first and the third lines are the beginning and the end of the "Release" event (discussed in property "Visible"). The second line sets our Dynamic Text’s value to the mouse coordinates using "_xmouse" and "_ymouse" properties. So when you click the button you will see the coordinates of the mouse in the Dynamic Text. But the null point is the center of the object. So if you click the left side of the button, the x coordinate will be less than 0 and if you click right side of the object, then the x coordinate will be more than 0.

CurrentFrame[edit]

This property tells you which frame of current Movie Clip is being played now. I wrote Movie Clip instead of object, because this property is available only for Movie Clip. This time we will create more complicated example.

Step 1

Select the Rectangle Tool Mfas image004.jpg in the Toolbox, draw a rectangle, double click it and press F8 to convert it to symbol. When the "Convert to Symbol" dialog appears, set its’ name "g_box" and behavior "Graphic".

Mfas image007.jpg Mfas image008.jpg

Step 2

Press F11 to see the Library window and click "Create New Symbol" Mfas image009.jpg. When the "Create New Symbol" dialog appears, set its’ name to "m_box" and the behavior to "Movie Clip" and press OK. When you are in "m_box" Mfas image010.jpg, add "g_box" graphic from the library. Then right click the 32-nd frame in Timeline window and choose "Insert Keyframe". Now click any frame between 1 and 32 and in the Properties window from the Tween combo box choose "Motion". Then in Rotate combo box choose "CW" (Clockwise) and in the "Rotation Count" field type 1. This will make the box 1 time to rotate in clockwise direction.

Step 3

Now go to the "Scene1" and select the Text Tool Mfas image006.jpg from the Toolbox. Draw a Text and set its’ width to 40, height to 20, font size to 20, Var to "info" and change the Text Type to "Dynamic Text".

Step 4

Now right click our box and choose "Actions". When the Actions windows appears make sure the "Advanced Mode" is turned on and type the following the movieclip m_box:

onClipEvent (enterFrame) {
   _root.info.text = _currentframe;
}

Now lets understand the code. The word "onClipEvent" is an event handler like "on". The word "enterFrame" is the event. If we use "on (event) {" in our previous examples for Buttons, then for Movie Clips we use "onClipEvent(event) {". Now lets discuss the second line. "_root" is the main object in the Flash. Imagine something like this: you are sitting in the car, the car is in the garage, the garage is in the building, the building is in the city. As you see the chief of all these is the city of course, the building is sub chief and so on. This mean that if you want to see the building you must first go out of the car, then out of garage, then out of the builing and only then you will see it. So if we programmly describe your position then it will be "_root.building.garage.car.you" where _root is the city. In our example we are in "onClipEvent (enterFrame) " event, so if we want to access var info, then we will have to access if via "_root", this means "_root.info". Now you ask why we write "_currentframe" without anything. Because we are already in the Movie Clip. For example if you are sitting in the car and you need to take keys from it. You will not get out of the car, then again enter it to take the keys, you will just simply take them and then get out. So that’s why in our example we use "_currentframe" without anything. But if you want to access it via "_root" then you will write "_root.box._currentframe".

Step 5

Now test the movie and you will see the result.

TotalFrames[edit]

_totalframes property just tells you how many frames are in the Movie Clip. For testing it we will use the previous example. Right click the keyframe in the Timeline window in Scene1 and choose "Actions". Then write the following in the Script pane:

trace(box._totalframes);

Trace is used to record programming notes or to display messages in the Output window while testing a movie. The use of trace is the following "trace(expression);". For example if you write "trace ("hello!!!");" then "hello!!!" will be displayed in the Output window. But this action is working only in Macromedia Flash MX. I mean that if you publish your movie, the user won’t see the Output window. So use it just for yourself to test the movies. Now lets go back to our example. This code will write the frames count to the Output window, and it will be 32.

URL[edit]

_url property retrieves the URL of the SWF file from which the movie clip was downloaded. As in the previous property add "trace(box._url)" and you will see the path of the SWF file.

Target[edit]

This returns the target path of the Movie Clip. This is nearly what we discussed in "TotalFrames". For example as we mentioned there, if we were in car then our target will be "\building\garage\car". Or another example: if you create a Movie Clip and set its name "m1" and then create another Movie Clip and put the previous Movie Clip in this one and name the new one "m2" then if in "m1" actions you write:

onClipEvent (enterFrame) {
   trace(_target);
}

then the return path will be "\m2\m1"

DropTarget[edit]

Discussed later.

FocusRect[edit]

This is a Boolean (true, false) value that specifies whether a movie clip has a yellow rectangle around it when it has keyboard focus. If the focus is set, then it returns true else false.

FramesLoaded[edit]

The property _framesloaded tells how many frames of the movie were loaded. This is very useful to monitor the download process.

SoundBufTime[edit]

Discussed later.

<< PreviousNext >>