Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 636063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:28:26+00:00 2026-05-13T20:28:26+00:00

i’m loading several sound files, and want to error check each load. however, instead

  • 0

i’m loading several sound files, and want to error check each load. however, instead programming each one with their own complete/error functions, i would like them to all use the same complete/error handler functions.

a successfully loaded sound should create a new sound channel variable, while an unsuccessfully loaded sound will produce a simple trace with the name of the sound that failed to load. however, in order to do this, i need to dynamically create variables, which i haven’t yet figured out how to do.

here’s my code for my complete and error functions:

function soundLoadedOK(e:Event):void
 {
 //EX: Sound named "explosion" will create Sound Channel named "explosionChannel"
 var String(e.currentTarget.name + "Channel"):SoundChannel = new SoundChannel();
 }

function soundLoadFailed(e:IOErrorEvent):void
 {
 trace("Failed To Load Sound:" + e.currentTarget.name);
 }

-=-=-=-=-=-=-=-=-
UPDATED (RE: viatropos)
-=-=-=-=-=-=-=-=-

can not find the error.

TypeError: Error #1009: Cannot access a property or method of a null object reference. at lesson12_start_fla::MainTimeline/loadSounds() at lesson12_start_fla::MainTimeline/frame1():

//Sounds
var soundByName:Object = {};
var channelByName:Object = {};
var soundName:String;
var channelName:String;
loadSounds();

function loadSounds():void
{
    var files:Array = ["robotArm.mp3", "click.mp3"];
    var i:int = 0;
    var n:int = files.length;
    for (i; i < n; i++)
    {
        soundName = files[i];
        soundByName[soundName] = new Sound();
        soundByName[soundName].addEventListener(Event.COMPLETE, sound_completeHandler);
        soundByName[soundName].addEventListener(IOErrorEvent.IO_ERROR, sound_ioErrorHandler);
        soundByName[soundName].load(new URLRequest(soundName));
    }
}

function sound_completeHandler(event:Event):void
{
    channelName = event.currentTarget.id3.songName;
    channelByName[channelName] = new SoundChannel();
}

function sound_ioErrorHandler(event:IOErrorEvent):void
{
    trace("Failed To Load Sound:" + event.currentTarget.name);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:28:27+00:00Added an answer on May 13, 2026 at 8:28 pm

    You can do this a few ways:

    1. Storing your SoundChannels in an Array. Good if you care about order or you don’t care about getting them by name.
    2. Storing SoundChannels by any name in an Object. Good if you want to easily be able to get the by name. Note, the Object class can only store keys ({key:value} or object[key] = value) that are Strings. If you need Objects as keys, use flash.utils.Dictionary, it’s a glorified hash.

    Here’s an example demonstrating using an Array vs. an Object.

    var channels:Array = [];
    // instead of creating a ton of properties like
    // propA propB propC
    // just create one property and have it keep those values
    var channelsByName:Object = {};
    
    function loadSounds():void
    {
        var files:Array = ["soundA.mp3", "soundB.mp3", "soundC.mp3"];
        var sound:Sound;
        var soundChannel:SoundChannel;
        var i:int = 0;
        var n:int = files.length;
        for (i; i < n; i++)
        {
            sound = new Sound();
            sound.addEventListener(Event.COMPLETE, sound_completeHandler);
            sound.addEventListener(IOErrorEvent.IO_ERROR, sound_ioErrorHandler);
            sound.load(files[i]);
        }
    }
    
    function sound_completeHandler(event:Event):void
    {
        // option A
        var channelName:String = event.currentTarget.id3.songName;
        // if you want to be able to get them by name
        channelsByName[channelName] = new SoundChannel();
    
        // optionB
        // if you just need to keep track of all of them,
        // and don't care about the name specifically
        channels.push(new SoundChannel())
    }
    
    function sound_ioErrorHandler(event:IOErrorEvent):void
    {
        trace("Failed To Load Sound:" + event.currentTarget.name);
    }
    

    Let me know if that works out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.