1
Actionscript for Flash Actionscript for Flash by Dr SC Liby Dr SC Li
2
Understanding more about instancesUnderstanding more about instances
SymbolsSymbols
graphicsgraphics
buttonsbuttons
Movie clipsMovie clips
Instances Instances (without (without names)names)
No No interactioninteraction
Vector GraphicsVector Graphics TextText
dynamicdynamicstaticstatic
Variable Variable namesnames
Instances Instances (without (without names)names)
Mouse Mouse interaction interaction
onlyonly
Instances Instances (with (with names)names)
MovieClip MovieClip interactioninteractioninput
Variable Variable namesnames
3
Paths to InstancesPaths to Instances
Path to instances residing on Clip2: Path to instances residing on Clip2: _root.Mary.John_root.Mary.John
Main Stage
Mary
John
4
Paths to InstancesPaths to Instances
Path to the text object: Path to the text object: _root.level1.level2.level3.text_root.level1.level2.level3.text
Dynamic text field with variable name: ”text””text”
Main StageMain Stage
level1level1
level2level2
level3level3
5
on (mouseEvent)on (mouseEvent)
Syntax Syntax on (mouseEvent) { ……….. ……….. ……….. }
Important on(mouseEvent) ACTION can only be
applied to BUTTON instances
6
On (mouseEvent)On (mouseEvent)
rollOver
rollOut dragOver
dragOut
press release releaseOutsidekeyPress
7
on (mouseEvent)on (mouseEvent)
press The mouse button is pressed while the pointer is
over the button.
release The mouse button is released while the pointer is
over the button.
releaseOutside The mouse button is released while the pointer is
outside the button.
8
on (mouseEvent)on (mouseEvent)
rollOver The mouse pointer rolls over the button.
rollOut The pointer rolls outside of the button area.
dragOver While the pointer is over the button, the mouse
button has been pressed while rolled outside the button, and then rolled back over the button.
9
on (mouseEvent)on (mouseEvent)
dragOut While the pointer is over the button, the mouse
button is pressed and then rolls outside the button area.
keyPress ("key") The specified key is pressed. The key portion of
the argument is specified using any of the key codes listed in the Appendix B, "Keyboard Keys and Key Code Values," or any of the key constants listed in the Property summary for the Key object.
10
OnClipEvent(???)OnClipEvent(???)
Any of the following values can be specified for the movieEvent argument:
load The action is initiated as soon as the movie clip
is instantiated and appears in the Timeline.
unload The action is initiated in the first frame after the
movie clip is removed from the Timeline. The actions associated with the Unload movie clip event are processed before any actions are attached to the affected frame.
11
OnClipEvent(???)OnClipEvent(???)
enterFrame The action is initiated as each frame is played,
similar to actions attached to a movie clip. The actions associated with the OnEnterFrame movie clip event are processed after any actions that are attached to the affected frames.
mouseMove The action is initiated every time the mouse is
moved. Use the _xmouse and _ymouse properties to determine the current mouse position.
12
OnClipEvent(???)OnClipEvent(???)
Syntax Syntax onClipEvent (movieEvent) { ……….. ……….. }
A movieEvent is a trigger event that executes actions that are assigned to a movie clip instance.
13
OnClipEvent(MovieEvent)OnClipEvent(MovieEvent)
load
unload enterFrame
data
mouseMove mouseUp mouseDown keyDownkeyUp
14
OnClipEvent(???)OnClipEvent(???)
Any of the following values can be specified for the movieEvent argument:
load The action is initiated as soon as the movie clip
is instantiated and appears in the Timeline.
unload The action is initiated in the first frame after the
movie clip is removed from the Timeline. The actions associated with the Unload movie clip event are processed before any actions are attached to the affected frame.
15
OnClipEvent(???)OnClipEvent(???)
enterFrame The action is initiated as each frame is played,
similar to actions attached to a movie clip. The actions associated with the OnEnterFrame movie clip event are processed after any actions that are attached to the affected frames.
mouseMove The action is initiated every time the mouse is
moved. Use the _xmouse and _ymouse properties to determine the current mouse position.
16
OnClipEvent(???)OnClipEvent(???)
mouseDown The action is initiated when the left mouse
button is pressed.
mouseUp The action is initiated when the left mouse
button is released.
keyDown The action is initiated when a key is pressed.
Use the Key.getCode method to retrieve information about the last key pressed.
17
OnClipEvent(???)OnClipEvent(???)
keyUp The action is initiated when a key is released.
Use the Key.getCode method to retrieve information about the last key pressed.
data The action is initiated when data is received in a
loadVariables or loadMovie action. When specified with a loadVariables action, the data event occurs only once, when the last variable is loaded. When specified with a loadMovie action, the data event occurs repeatedly, as each section of data is retrieved.
18
Flash ActionsFlash Actions
19
getProperty/setPropertygetProperty/setProperty properties of an instanceproperties of an instance
_xscale, _yscale, _x, _y, _alpha, _rotation, ……….. etc.
actionscript (for a movie clip): onClipEvent (mouseDown) { dummy=getProperty(_target, _alpha)-10; setProperty(_target, _alpha, dummy); }
20
getProperty/setPropertygetProperty/setProperty properties of an instanceproperties of an instance
_xscale, _yscale, _x, _y, _alpha, _rotation, ……….. etc.
actionscript (for a button): on (press) { dummy=getProperty(_target, _alpha)-10; setProperty(_target, _alpha, dummy); }
21
IF-ELSE statementsIF-ELSE statements
if(conditions){ statements… } else { statements … }
Example if(dummy < 0){ setProperty(_target, _alpha, 100); } else { setProperty(_target, _alpha, dummy); }
22
Actionscript: Exercise 1Actionscript: Exercise 1
Changing the property of an instance (1)Changing the property of an instance (1) Open the file action-ex1.fla Open the library window by clicking
Windows > LibraryWindows > Library Drag the movie clip “mouse” from the library
panel onto the main stage Assign a name to the movie clip instance by
highlighting it, then by right clicking it. Choose Panels > InstancePanels > Instance and on the
“name field”, type in the name for the instance.
23
Actionscript: Exercise 1Actionscript: Exercise 1
Insert an actionscript for the movie clip instance by highlighting it, then by right clicking it.
Choose Actions Actions and change the script editor to expert mode. Then type in the script:
onClipEvent (mouseDown) { dummy=getProperty(_target, _alpha)-10; setProperty(_target, _alpha, dummy); }
Save the file and preview it.
24
Actionscript: Exercise 2Actionscript: Exercise 2
Changing the property of an instance (2)Changing the property of an instance (2) Compare and contrast the two Flash movies:
action-ex1-finished.swf and action-ex2-finished.swf
Open the file action-ex2.fla Open the library window the library window
by clicking Windows > LibraryWindows > Library Insert a new “Movie Clip” symbol by clicking
Insert > New SymbolsInsert > New Symbols Drag the button “mouse_b” from the library
panel onto the movie clip stage
25
Actionscript: Exercise 2Actionscript: Exercise 2 Insert an actionscript for the button instance
on movie clip stage by highlighting it, then by right clicking it.
Then turn to Scene 1 (the main stage) and drag the movie clip, “mouse” from the library panel onto the main stage.
Assign a name to the movie clip instance by highlighting it, then by right clicking it.
26
Actionscript: Exercise 2Actionscript: Exercise 2
Choose Panels > InstancePanels > Instance and on the “name field”, type in the name, “mouse” for the instance.
Choose Actions Actions and change the script editor to expert mode. Then type in the script:
on (press) { dummy=getProperty(_target, _alpha)-10; setProperty(_target, _alpha, dummy); }
Save the file and preview it.
27
Actionscript: Exercise 3Actionscript: Exercise 3
Changing the property with IF-ELSE Changing the property with IF-ELSE Open the file action-ex3.fla Open the library window by clicking
Windows > LibraryWindows > Library Double-click the Movie Clip “mouse” On the stage of the movie clip “mouse”,
modify the actionscripts for the button instance as:
28
Actionscript: Exercise 3Actionscript: Exercise 3
Actionscript: on (press) { dummy=getProperty(_target, _alpha)-10;
if(dummy < 0){ setProperty(_target, _alpha, 100); } else { setProperty(_target, _alpha, dummy); } }
Save the file and preview it.
29
Actionscript: Exercise 4Actionscript: Exercise 4
Changing the position of an instance Changing the position of an instance Open the file action-ex4.fla Follow similar steps given in exercise 2,
modify the actionscript so that after clicking on the instance, its x position will be changed.
30
Actionscript: Exercise 5Actionscript: Exercise 5
Changing the orientation of an instance Changing the orientation of an instance Open the file action-ex5.fla Follow similar steps given in exercise 2,
modify the actionscript so that after clicking on the instance, it will be rotated to a certain degree.
31
Actionscript: Exercise 6Actionscript: Exercise 6
Displaying the property of an instance Displaying the property of an instance Open the file action-ex6.fla On the main stage, create dynamic text fields
with appropriate names for the corresponding property items
Insert an actionscript for the button instance on the movie clip stage of “mouse” so that on clicking the instance, its properties will be displayed on the text boxes.
32
Actionscript: Exercise 6Actionscript: Exercise 6
Actionscript: on (release) { _root.xpos = getProperty ( _target, _x ); _root.ypos = getProperty ( _target, _y ); _root.alpha = getProperty ( _target, _alpha ); _root.rotation = getProperty ( _target, _rotation
); }
Save the file and preview it.
33
Actionscript: Exercise 7Actionscript: Exercise 7
Changing the property of an instance (3)Changing the property of an instance (3) Open the file action-ex7.fla On the main stage, create dynamic text fields
with appropriate names for the corresponding property items
Insert an actionscript for the yellow button instances on the main stage of so that on clicking the yellow button, the corresponding property of the “mouse” instance will be changed subsequently.
34
Actionscript: Exercise 7Actionscript: Exercise 7
Actionscript (for example): on (release) { setProperty (_root.mouse, _x, xpos); }
Save the file and preview it.
35
Actionscript: Exercise 8Actionscript: Exercise 8
Creating a draggable objectCreating a draggable object Open the file action-ex8.fla Open the library window by clicking
Windows > LibraryWindows > Library Edit the movie clip “mouse” by double-
clicking the clip on the library panel On the stage for the mouse clip, there is an
instance of the button “mouse_b”. Insert a mouse action for the button as follows:
36
Actionscript: Exercise 8Actionscript: Exercise 8
Change the script editor to expert mode type in the script:
on (press) { startDrag(this, true); } on (release){ stopDrag(); }
Save the file and preview it.
Top Related