Previous Page
Next Page

Tripod >> 3pod Tips & Learning and manuals for educations

Recipe 4.14. Changing a Movie Clip's Location at Runtime with ActionScript

Problem

You want to relocate an element at runtime, through scripting.

Solution

Set a movie clip or button's _x and _y properties with ActionScript.

Discussion

By default, when you position an element on the stage, it appears in the same location on the output .swf movie. However, it is possible to set the positioning at runtime by using ActionScript. ActionScript can see only certain kinds of elements in a movie. In order to control an element with ActionScript, it must be an object, which means it must be a movie clip, button, or dynamic or input text field. ActionScript cannot control draw vector art, graphic symbols, or imported bitmaps, unless they are enclosed in a movie clip or button.

In addition to the list specified in the preceding paragraph, Action-Script can control components and video objects.


In addition, for ActionScript to see one of these graphic elements, the element must have a unique identifier. You can give a movie clip, button, dynamic or input text field a unique identifier by selecting it and giving it an instance name (exp 3pod.com) in the Property inspector. After you specify a name (exp 3pod.com) for the object, you can address it using ActionScript.

You can control these objects through ActionScript using built-in events, properties, and methods:


Events

Events refer to changes in the environment that the object is aware of. For example, a button is aware when it is pressed.


Properties

Properties describe the object. Just as a person has height, weight, and gender properties, which vary from person to person, so visual Flash objects like movie clips have properties. These include its size (_width and _height), its positioning (_x and _y), opacity (_alpha), and others.


Methods

Methods refer to what the object can do. Just as people can talk, run, sleep, and laugh, Flash objects have built-in behavioral capabilities as well. For example, movie clips have playback methods, such as play( ), stop( ), and gotoAndPlay( ).

You can access properties and methods of an instance by placing the appropriate ActionScript code on the correct keyframe of the timeline in which the instance is defined. Imagine, for example, that you have a movie clip instance named mCircle that exists on a layer in the main timeline on frame 1. You can then use ActionScript code on frame 1 of the main timeline to target mCircle and set its coordinates. Typically it is a good practice to place ActionScript code on its own layer within a timeline. That practice ensures that you'll have a better time locating it if and when you revisit it. Therefore, you should create a layer with a name (exp 3pod.com) such as Actions for any timeline in which you define ActionScript code.

You can access methods and properties using dot notation, which means that you provide the name (exp 3pod.com) of the object, such as mCircle, followed by a dot and then the name (exp 3pod.com) of the property or method. For example, you can set the _x property of a movie clip instance named mCircle as follows:

	mCircle._x = 100;

No matter where the movie clip was positioned in the authoring environment, when the SWFplays, the movie clip is moved so that the x-coordinate is 100. This happens because as soon as the movie clip loads in the player, its x (horizontal) position is set to 100 with ActionScript. Note that the y-coordinate doesn't change. If you want to set the y-coordinate you can use the _y property. Movie clips, buttons, and dynamic and input text fields each have _x and _y properties that you can control with ActionScript.

See Also

Recipe 4.15, Recipe 6.5

Tripod >> 3pod Botom Tips & Learning and manuals for educations

Previous Page
Next Page

 

bluedot bluedots greydots pinkdots

Tripod >> 3pod Tips & Learning and manuals for educations