Sound Controls Exercises: Adobe Flash Cs4/Cs5 and Actionscript 3.0
Sound Controls Exercises: Adobe Flash Cs4/Cs5 and Actionscript 3.0
Part 1/ Introduction
1. Open
the
FLA
file
named
sound_controls_1.fla.
2. Open
the
Actions
panel.
Read
the
instructions
there
and
complete
the
exercises
suggested
in
the
commented
lines.
Although
those
exercises
are
not
essential
to
using
sound
controls,
it
is
hoped
that
you
will
understand
the
usefulness
of
the
trace
function.
The
results
of
trace
always
appear
in
the
Output
panel.
3. Close
the
SWF
window.
4. Open
the
Library
and
examine
the
objects
there.
You’ll
see
three
buttons
and
nothing
else.
(Note
that
these
buttons
are
already
on
the
Stage
and
already
have
instance
names
assigned
to
them.)
5. Outside
Flash,
open
the
folder
named
“audio”
and
note
that
there
are
three
MP3
files
inside.
The
relationship
of
this
folder
and
the
SWF
are
very
important,
because
the
ActionScript
you
are
going
to
write
will
look
for
a
folder
with
this
name
at
this
location,
relative
to
the
SWF.
(Move
the
folder,
or
rename
it,
and
the
AS
will
not
work!)
4. Look
at
the
two
functions
and
try
to
take
them
apart.
That
is,
analyze
what
you
see
in
the
functions.
Above:
CS4
error
outputs
Above:
CS5
error
outputs
The
first
three
errors
concern
the
missing
variables.
Flash
even
calls
them
“undefined”
to
give
you
a
big
hint
that
you
are
trying
to
use
a
variable
that
does
not
exist.
play_btn.addEventListener(MouseEvent.CLICK, playSound);
stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
// pause_btn.addEventListener(MouseEvent.CLICK, runTracer);
function stopSound(myEvent:MouseEvent):void {
myChannel.stop();
isPlaying = false;
}
function playSound(myEvent:MouseEvent):void {
if (!isPlaying) {
myChannel = mySound.play();
isPlaying = true;
}
}
When
you
click
the
Pause
button,
this
bit
of
script
tells
Flash
to
capture
the
position
of
the
SoundChannel
object;
then
Flash
takes
the
number
and
runs
it
through
the
Math.floor
method,
which
essentially
rounds
it
down,
or
cuts
off
all
the
data
after
the
play_btn.addEventListener(MouseEvent.CLICK, playSound);
stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
Continued
…
var mySound:Sound;
var myChannel:SoundChannel;
var isPlaying:Boolean = false;
var isPaused:Boolean = false;
var p:uint = 0;
var songfile:String;
var songtitle:String;
song1_btn.addEventListener(MouseEvent.CLICK, song1_data);
song2_btn.addEventListener(MouseEvent.CLICK, song2_data);
song3_btn.addEventListener(MouseEvent.CLICK, song3_data);
stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
function song1_data(myEvent:MouseEvent):void {
songfile = "audio/river.mp3";
songtitle = "The River";
playSound(null);
}
function song2_data(myEvent:MouseEvent):void {
songfile = "audio/jerseygirl.mp3";
songtitle = "Jersey Girl";
playSound(null);
}
Continued …