Sound with ActionScript - Flash Storybook App

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.

  • 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

    IMPORTANT: These app development tutorials are written with the Flash novice in mind. You will need to optimize your app beyond what is covered in these tutorials before finalizing your app for the app store.


    Copyright 2018 Adobe Systems Incorporated. All rights reserved. Adobe product screen shot(s) reprinted with permission from Adobe Systems Incorporated. Adobe, Photoshop, Photoshop Album, Photoshop Elements, Illustrator, InDesign, GoLive, Acrobat, Cue, Premiere Pro, Premiere Elements, Bridge, After Effects, InCopy, Dreamweaver, Flash, ActionScript, Fireworks, Contribute, Captivate, Flash Catalyst and Flash Paper is/are either [a] registered trademark[s] or a trademark[s] of Adobe Systems Incorporated in the United States and/or other countries.





  • RSS
    Related Articles
    Editor's Picks Articles
    Top Ten Articles
    Previous Features
    Site Map








    Content copyright © 2023 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.