6 Replies Latest reply: May 7, 2012 8:48 PM by Mr. Baker the Shoe Maker RSS

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

    Mr. Baker the Shoe Maker Community Member

      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.

        • 1. Re: Error #1009 when playing sound inside a movie clip that is called to stage with add child method
          Ned Murphy CommunityMVP

          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.

          • 2. Re: Error #1009 when playing sound inside a movie clip that is called to stage with add child method
            iFlashAppsToo Community Member

            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?)

            • 3. Re: Error #1009 when playing sound inside a movie clip that is called to stage with add child method
              Ned Murphy CommunityMVP

              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.

              • 4. Re: Error #1009 when playing sound inside a movie clip that is called to stage with add child method
                Mr. Baker the Shoe Maker Community Member

                Ned,

                 

                I'm not quite sure what you mean by "If the intention is to only call the function once, then just call the function...

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

                 

                ___________________

                 

                Yes, my intention is to only play the sound once. I don't understand the options within functions fully at this point in my programming.

                 

                 

                //Plays music

                stage.addEventListener(Event.ENTER_FRAME, startRJSong);

                function startRJSong(event:Event):void

                {

                 

                    RJPlaySongChannel = RJPlaySong.play();

                 

                }

                • 5. Re: Error #1009 when playing sound inside a movie clip that is called to stage with add child method
                  Ned Murphy CommunityMVP

                  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();

                  • 6. Re: Error #1009 when playing sound inside a movie clip that is called to stage with add child method
                    Mr. Baker the Shoe Maker Community Member

                    Hi Ned,

                     

                    Thanks for sticking with me on this one.

                     

                    The code works but now it starts at the beginning of the movie, not when it is called in the movie clip. Can't figure out why it would play when it is inside of the movieclip (and has not been called.

                     

                    FYI, I have a music (instrumental) that plays when the move opens.


                    var RayJayInstrumentalSong:RayJayInstrumental;

                    var RayJayInstrumentalChannel:SoundChannel;

                     

                    for (var i:Number=0; i<=1; i++)

                    {

                     

                        RayJayInstrumentalSong = new RayJayInstrumental();

                        RayJayInstrumentalChannel = RayJayInstrumentalSong.play();

                    }

                     

                     

                    if (i<=1)

                    {

                        RayJayInstrumentalSong = null;

                        RayJayInstrumentalChannel = null;

                    }

                     

                     

                    Here is the code I put in the movieclip that is called:

                     

                    stop();

                     

                    //Variables for the playing of music

                    var RJPlaySong:RJSong = new RJSong();

                    var RJPlaySongChannel:SoundChannel;

                     

                    //Plays music

                    function startRJSong():void

                    {

                     

                        RJPlaySongChannel = RJPlaySong.play();

                     

                    }

                     

                    startRJSong();

                     

                     

                    I checked that the class names are different in the movie clip's libray properties. I can't think of anything else.