| Ultrashock Tutorials > Flash4 > Actionscript Quicktips | |||||
|
|||||
| Actionscript Quicktips | |||||
Actions LayerIt is always a smart idea to keep your frame actions on their own layer. Otherwise you can end up affecting functionality when you reposition keyframes containing graphical elements. Similarly, it is a good idea to create separate layers for sound and labels.
In Flash the (x,y) point (0,0) marks the upper left corner of the screen. These values increase as you move down and to the right on the screen. So the bottom right corner of the screen might be (550,350). While the units in Flash are pixels, the coordinate system actually scales with the Flash movie. This means that you don't have to worry about the scale at which the movie is being viewed when writing your actionscript.
Drag Actions and Release Outside One common problem is to use On Release as the mouse event to stop a drag action. If the user manages to release the mouse button while not over the button they will be stuck with the movie clip attached to their mouse. To avoid this problem use On (Release, Release Outside).
Expressions vs. String Literals Probably the most common source of errors in actionscript involves setting a field as a string literal when it should be an expression. Expressions are used any time you want Flash to doing some sort of task to arrive at a value. This includes looking up a value stored in a variable or performing some kind of function or operation. Forgetting to set a field as an expression often returns a result of 0. For example: Set Variable: "newvalue" = "5*7+2" The variable "newvalue" does not contain 37, but instead the string "5*7+2". If you were to use that in a condition such as newvalue > 30, newvalue would be treated as 0.
There are a couple of shortcuts when working with the GetProperty action. First of all, remember that when you leave the target field blank it will assume you are talking about the current movie clip. Secondly, you can treat these properties as if they are read-only variables. For example, _name refers to the name of the current movie clip. But /:_name refers to the name of the parent timeline. For example, these statements are the same:
Set Variable: "x" = GetProperty("instance",_x)
Set Variable: "x" = instance:_x
A hidden button is one which contains a hit state but has an empty up, over and down state. This means that while it has an active area and can be associated with actions the button is invisible to the user. When working in Flash hidden buttons are displayed in a transparent cyan colour. Typically, you can create a single hidden button for your project and scale, skew and rotate it to fit.
Symbols exist in your library. Whenever you drag a symbol from your library on to your stage is creates a new instance of that symbol. Instances are always linked back to the original symbol. However, instances can vary in their position, alpha, tint, etc.
When Flash is viewed inside a browser it can only respond to keypresses when the focus is on the Flash movie. For example, normally pressing the down key in the browser will scroll the page down. If you wish the user to control the movie with keypresses you must get the user to click on the Flash movie first. One way of ensuring the focus is on the flash movie is to use a "start by clicking here" button (see an example here). Another technique is to use a bit of javascipt to do the job for you. Here is a sample movie which shows this in action. View the source to see how the javascript is setup. Remember that the flash file must be named for this to work. The focus method only seems to work with flash files in Internet Explorer.
A flash player window can play several .swf files at once. Additional movies are loaded using the Load Movie action and exist on their own levels. The starting movie is always loaded on level 0 and higher levels display on top of this level. A level can only contain a single .swf file. The movie background for levels above 0 are not relevant since the background is left transparent. Although it is possible to have many levels loaded at once, designing sites this way can be a challenge since you are working with many individual flash files. See Working with Levels for more information.
There is no direct way to read the x and y position of the pointer in Flash. However, the same effect can be achieved by attaching a small movie clip to the mouse and then reading its x and y position.
To see how this works download the source. In the first frame I start a drag action on a movie clip instance called mouse.
Start Drag ("mouse", lockcenter)
Set Property ("mouse", Visibility) = "0"
The middle frame constantly reads the x and y position of
the movie clip.
Set Variable: "locV" = mouse:_y Set Variable: "locH" = mouse:_x The mouse speed can be calculated by comparing the current x and y position of a dragged movie clip to the values from the last frame. This code can be encapsulated inside of a movie clip and easily reused. Tracking the mouse speed is used to allow the user to "throw" and object such as with a slider bar or bouncing ball. See article 3 of the Animation Through Actionscript article for an example.
There is no "play in reverse" action available in actionscript. However, you can create the same effect using a simple three-frame movie clip. The movie clip can be called on to send a continuous stream of "go to previous" actions to the timeline containing the animation. Here is a simple example with source.
Targets are used to identify a particular timeline within a Flash file. They are needed to pass instructions from one timeline to another or to get the property of a specific movie clip instance. Targets use instance names to identify specific timelines. Targets are similar to the paths used in file systems and come in two forms: relative and absolute. Absolute paths always start with a leading forward slash "/". They identify a particular timeline in relation to the parent timeline. For example: "/instance1" or "/instance1/instance2". Relative paths identify other timelines in relation to the current timeline. The special notation ".." refers to the timeline one level up. Examples: "instance1/instance2" or "../smallthing". In most cases, it is better to use relative paths. For more information see the article on targets.
Text fields allow you to get keyboard input or display the results of an expression or text loaded from a text file or database. Every text field is associated with a variable and displays the contents of that variable. Text fields do not allow for any kind of formatting (bold, italic, font changes, etc.) within them. You can selectively embed the font outlines when working with text fields. Including the complete outlines for a font adds roughly 20-30K to your file. If you choose not to include font outlines you should use one of the predefined fonts (_sans, _serif, _typewriter). Note that to stretch, scale, skew or animate a text field you must embed the font.
The three frames are used like this: Frame 1 contains setup code such as establishing or resetting variables, Frame 2 contains the set of actions to be repeated every frame and Frame 3 contains a Goto Frame 2 and Play action.
|
|||||
|
©2000 Ultrashock.com Inc.
- All rights reserved
|