g
Printer Friendly Version

editor  
BellaOnline's Flash and Animation Editor
 

Sound with ActionScript - Flash Storybook App

    With that in mind, we want the reader to be able to play the sound file immediately when the app begins. The ActionScript that plays the sound must run when the app loads. To be sure the code runs immediately, we will place the code on the first frame of the Timeline.

    Note: Any code placed on other frames of the Timeline will run when Flash enters that frame. This is one way to control the timing for your code.

  1. Add a new layer at the top of the Timeline and name it "actions". Click on the first frame of the actions layer and then open the Actions Panel. Type the following code in the blank box.

// redOrn button

redOrn.addEventListener(MouseEvent.CLICK, redOrnSound);

function redOrnSound (event:MouseEvent):void {

var jinglbellSound:jinglbell = new jinglbell();
var jinglbellChannel:SoundChannel = new SoundChannel();
jinglbellChannel = jinglbellSound.play();
jinglbellChannel = null;
jinglbellSound = null;

}

Many effects in Flash are controlled by Event Listeners, which listen for a certain event (such as a mouse click) and then run a section of code called a function. Above, we first have added an Event Listener to the redOrn movie clip that will run the function called redOrnSound.

In the reOrnSound function is the code that will play the sound file named jinglbell.

  1. The first line of code in the function creates a variable to hold the name of the sound file and a new instance of the sound.

    var jinglbellSound:jinglbell = new jinglbell();

  2. The second line of code creates a variable to hold the name of the sound channel for the sound and creates a new instance of the sound channel.

    var jinglbellChannel:SoundChannel = new SoundChannel();

  3. The third line of code associates the sound channel to the sound and plays the sound.

    jinglbellChannel = jinglbellSound.play();

    All that just to play a simple sound. Welcome to object-oriented programming.

  4. The last two lines of code set the two variables to null to save memory storage space for our app.

    jinglbellChannel = null;
    jinglbellSound = null;

Repeat these steps for the other two ornaments.

More about symbols: Symbols are the building blocks of Flash. There are three types of symbols, movie clip, button and graphic. The button symbol is different from the other two because it has a special four-frame Timeline. The other two symbol types have a Timeline like the main Timeline that we have been working on. For simple buttons, using the Button symbol and its special Timeline is sufficient. But for complex buttons such as the one we created above, we need to add ActionScript to make the movie clip function as a button.

← Back


|
Join us in the Digital Art and Design forum. | Join us in the Flash forum.


Flash and Animation Site @ BellaOnline
View This Article in Regular Layout

Content copyright © 2013 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Diane Cipollo for details.



| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2014 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor