Skip navigation
Currently Being Moderated

Error #1009 when playing sound inside a movie clip that is called to stage with add child method

May 4, 2012 11:06 AM

Tags: #error #sound #movieclips ##1009

What is supposed to happen

I have a movie clip that is called when the user clicks a button. When the move clip appears music is supposed to play. When the user clicks the close button the music stops and the movie clip is removed from the stage.

 

But then I get an error

When I test the movie I get the following error:

 

TypeError: Error #1009: Cannot access a property or method of a null object reference.

    at mc_playSong/frame2()[mc_playSong::frame2:6]

 

 

This is the code that calls the movie clip to the stage (no problems here):

// Calls movie clip with song playing to the stage

stage.addEventListener(MouseEvent.MOUSE_DOWN, goButtons);

function goButtons(event:MouseEvent):void

{

   

    if (event.target == song_bnt)

    {

        SoundMixer.stopAll();

        addChild(myPlaySong);

        myPlaySong.x = 558;

        myPlaySong.y = 384;

        //event.stopImmediatePropagation();

    }

 

 

Here's the code inside the Movie clip that is called to the stage. Music is supposed to play:

 

stop();

 

//Variables for the playing of music

var RJPlaySong:RJSong = new RJSong();

var RJPlaySongChannel:SoundChannel;

 

//Plays music

stage.addEventListener(Event.ENTER_FRAME, startRJSong); (I believe the error in the code is in this function. When I comment it out i don't get the error.)

function startRJSong(event:Event):void

{

 

    RJPlaySongChannel = RJPlaySong.play();

 

}

 

//Stops all sounds, sets variables to null, and calls a dispatch event that removes the clip from the stage.

bnt_closeSong.addEventListener(MouseEvent.MOUSE_DOWN, closeCreditSongScreen);

function closeCreditSongScreen(event:MouseEvent):void

{

    SoundMixer.stopAll();

    //RJPlaySong = null;

    //RJPlaySongChannel = null;

    dispatchEvent(new Event ("RemoveMCsong"));

 

}

 

Does anyone have any thoughts? This seems to be a pretty simple issue but I can't figure it out.

 
Replies
  • Currently Being Moderated
    May 4, 2012 11:44 AM   in reply to Mr. Baker the Shoe Maker

    The line:

     

    stage.addEventListener(Event.ENTER_FRAME, startRJSong);

     

    Is probably not what you want to use.  The "ENTER_FRAME" event is not what its name implies.  What it does is continually fires off at the frame rate of your file.  So that line will constantly be calling the startRJSong() function.

     

    If the intention is to only call the function once, then just call the function...

     

    startRJSong();  // and remove the arguments from the function

     

    As far as the 1009 error goes...

     

    The 1009 error indicates that one of the objects being targeted by your code is out of scope.  This could mean that the object....

     

    - is declared but not instantiated

    - doesn't have an instance name (or the instance name is mispelled)

    - does not exist in the frame where that code is trying to talk to it

    - is animated into place but is not assigned instance names in every keyframe for it

    - is one of two or more consecutive keyframes of the same objects with no name assigned in the preceding frame(s).

     

    While it appears you might have already done so, if you go into your Publish Settings Flash section and select the option to Permit Debugging, your error message should have a line number following the frame number which will help you isolate which object is involved.

     

    I can see your error is indicating it has to do with line 6 in frame 2 of what I am guessing is your movieclip.

     
    |
    Mark as:
  • Currently Being Moderated
    May 4, 2012 3:02 PM   in reply to Mr. Baker the Shoe Maker

    Here's the code inside the Movie clip that is called to the stage. Music is supposed to play:

     

    stop();

     

    //Variables for the playing of music

    var RJPlaySong:RJSong = new RJSong();

    var RJPlaySongChannel:SoundChannel;

     

    //Plays music

    RJPlaySongChannel = RJPlaySong.play();

     

    //Stops all sounds, sets variables to null, and calls a dispatch event that removes the clip from the stage.

    bnt_closeSong.addEventListener(MouseEvent.MOUSE_DOWN, closeCreditSongScreen);

    function closeCreditSongScreen(event:MouseEvent):void

    {

         RJPlaySongChannel.stop();

        SoundMixer.stopAll();

        RJPlaySongChannel = null;

        RJPlaySong = null;

        dispatchEvent(new Event ("RemoveMCsong"));

    }

     

    //---

     

    notice the bolded lines of code

    notice i flipped 2 lines in the closeCredit function and commented them back in

    the dispatchEvent may be causing your error (where is the function for RemoveMCsong?)

     
    |
    Mark as:
  • Currently Being Moderated
    May 4, 2012 4:55 PM   in reply to iFlashAppsToo

    The dispatchEvent line will not cause an error, it is an independent action.  The event being dispatched does not need a function, a listener for the event that is dispatched does.

     
    |
    Mark as:
  • Currently Being Moderated
    May 7, 2012 4:30 AM   in reply to Mr. Baker the Shoe Maker

    I meant what you just quoted to be taken literally.  If it is still not clear what I am saying or what your code is doing, try the following to demonstrate it... change that code to be...

     

    stage.addEventListener(Event.ENTER_FRAME, startRJSong);

    function startRJSong(event:Event):void {

        trace("I can do this all day");

    }

     

     

    After doing that you might see why you only want to call the function once, and you could eliminate most of it and just put...

     

    RJPlaySongChannel = RJPlaySong.play();

     

    in place of it all, or as I said originally you could change it to be...

     

    function startRJSong():void

    {

        RJPlaySongChannel = RJPlaySong.play();

    }

    startRJSong();

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points