4 Replies Latest reply on Jan 25, 2007 3:52 AM by Newsgroup_User

    Need Help in Sound Action Script...

    -wizz-
      Hi there,

      Actually I am experiencing some weird bizarre problem... I want to make a Sound object that does a normal player... I did the play, pause, and stop... Everything works properly... But when I go for next music (automatically called after current song finished) I should have make function sound complete in Sound object to do... But I cant... Because I couldnt find reference for my Class object... For example

      class MusicPlayer
      {
      var sound:Sound = new Sound();
      var songList:Array=new Array();
      var counter:Number=0;

      public function init():void
      {
      sound.onLoad=function()
      {

      }
      sound.onSoundComplete=function()
      {
      sound.load( songList[counter+1] );
      }
      }

      Of course, on the function onSoundComplete, the songList object will not recognized... So is there any way to make this working? Sometime I got annoyed because of lack of standardization in programming in flash... I hope in next version, it will be better! Thanks!
        • 1. Re: Need Help in Sound Action Script...
          Level 7
          Hi!

          There is a component class called Delegate which should be able to help you,
          if I am not mistaking. I also _think_ you could do something like:

          class MusicPlayer
          {
          var sound:Sound = new Sound();
          var songList:Array=new Array();
          var counter:Number=0;

          public function init():void
          {
          var thisClass = this;
          // *NEW*
          sound.onLoad=function()
          {

          }
          sound.onSoundComplete=function()
          {
          sound.load( thisClass.songList[counter+1] ); //
          *MODIFIED*
          }
          }

          /Jensen/

          "-wizz-" <webforumsuser@macromedia.com> wrote in message
          news:ep7nv5$14s$1@forums.macromedia.com...
          > Hi there,
          >
          > Actually I am experiencing some weird bizarre problem... I want to make a
          > Sound object that does a normal player... I did the play, pause, and
          > stop...
          > Everything works properly... But when I go for next music (automatically
          > called
          > after current song finished) I should have make function sound complete in
          > Sound object to do... But I cant... Because I couldnt find reference for
          > my
          > Class object... For example
          >
          > class MusicPlayer
          > {
          > var sound:Sound = new Sound();
          > var songList:Array=new Array();
          > var counter:Number=0;
          >
          > public function init():void
          > {
          > sound.onLoad=function()
          > {
          >
          > }
          > sound.onSoundComplete=function()
          > {
          > sound.load( songList[counter+1] );
          > }
          > }
          >
          > Of course, on the function onSoundComplete, the songList object will not
          > recognized... So is there any way to make this working? Sometime I got
          > annoyed
          > because of lack of standardization in programming in flash... I hope in
          > next
          > version, it will be better! Thanks!
          >


          • 2. Re: Need Help in Sound Action Script...
            Level 7
            > There is a component class called Delegate which
            > should be able to help you, if I am not mistaking.

            Delegate class would be great. It shipped with the 7.0.2 update to
            Flash MX 2004 and is still available in Flash 8.

            >> sound.onSoundComplete=function()
            >> {
            >> sound.load( songList[counter+1] );
            >> }

            The global "this" property (without quotes) points to the object or
            timeline it's in -- so in the above line, "this" inside that function would
            refer to your Sound instance, by virtue of being assigned to an event of
            that instance.

            sound.onSoundComplete = function():Void {
            trace(this);
            this.load(songList[counter+1]);
            }

            ... assuming a load() method is available (did you mean loadSound()?).

            >> Sometime I got annoyed because of lack of
            >> standardization in programming in flash... I
            >> hope in next version, it will be better! Thanks!

            Flash has a wild, varied history and has evolved considerably with each
            new release. There are pros and cons to everything, and the con here is
            that, as you correctly point out, the AS2 API is often more haphazard than
            it should be. But you can rest assured ActionScript 3.0 resolves these
            issues. :)


            David Stiller
            Adobe Community Expert
            Dev blog, http://www.quip.net/blog/
            "Luck is the residue of good design."


            • 3. Re: Need Help in Sound Action Script...
              -wizz- Level 1
              Wow! Awesome.... Jensen! It is working! But I just wonder... Why that can be working? Because according to the Flash "OO" The object function scope (in function soundComplete) will only contain any property n function for sound... Why the hell if you create new local variable, it will appear in the sound function scope? Can you explain to me a bit more detail? Because I really curious! LOL!

              David! Long time a go... I try that method... But somehow... The this will return sound reference... Because as far as I see... It quite a bit make sense... Where in sound function if you try to get reference this will return holder of that function which is sound object! So that I get confused and in that function just like isolated... And cannot do anything! But sometime I got annoyed... Altough Macromedia gives us a lot example in almost each function and object... They also almost gives us a simple "example" like... my problem... but the just call statement trace("Oh your object just call onSoundComplete"); lol! Bloody hell! I know that also! LOL! Anyway... But after I think again... Macromedia is the best one for animation design... Sooooo deep sad Macromedia is disappeared... I know Adobe is good company also! I love use their Adobe Photoshop! And I will say again! No substitute for image editor! hahaha!
              • 4. Re: Need Help in Sound Action Script...
                Level 7
                Hi again!

                I am glad it worked for you. I find it not so easy to explain why this
                works. I've created a simple example and played around with it to try and
                explain it... but not really been able to explain one of the key points.
                I'll include the discussion here, and then maybe others could fill in the
                last gap.

                I created the following simple example.
                1) Make a movieclip (MC) with a square shape
                2) Place an instance of the MC on root timeline and name it Box.
                3) Place the following code on root timeline:
                function setOnEnterFrame() {
                var nr = 8;
                Box.onEnterFrame = function() {
                trace(nr);
                }
                }
                setOnEnterFrame();

                Result is you get spammed with the value 8 in the trace window.

                Then I verified the scope of the variable by changing the code:
                function setOnEnterFrame() {
                var nr = 8;
                Box.onEnterFrame = function() {
                trace(nr);
                trace(this.nr);
                }
                }
                setOnEnterFrame();
                I get 8, undefined, 8, undefined, ...
                So nr is not in the Box scope (as I already expected).

                Then I verified that nr is not translated into its value when it is created,
                as might be the case or possible to do in other programming languages. If it
                was the case, then the first code would create the following function at
                runtime:
                Box.onEnterFrame = function() {
                trace(8)
                }
                I verified that this is not the case by changing the code to:
                function setOnEnterFrame() {
                var nr = 8;
                Box.onEnterFrame = function() {
                nr = nr*2;
                trace(nr);
                trace(this.nr);
                }
                }
                setOnEnterFrame();
                The output is:
                16
                undefined
                32
                undefined
                64
                undefined
                128
                ...
                This would not be the result if nr was translated to its value when the
                function was dynamically created. (nr = nr*2 -> 8 = 8*2 ... resulting in
                error, plus trace(nr) -> trace(8), would only give 8 as output anyways).

                So, I have verified and found out that
                1) nr is not in the scope of the Box. (Assumed to be still in the scope of
                the function).
                2) nr remains as a variable when the created function is run.

                So, the Flash compiler must be keeping track of the scope of nr for us.
                Furthermore, the variable is not terminated when setOnEnterFrame is finished
                running, as one might expect. Which is kind of what you pointed out and
                asked, and what I have a hard time explaining why is so ...

                /Jensen/


                "-wizz-" <webforumsuser@macromedia.com> wrote in message
                news:ep8kk0$4mh$1@forums.macromedia.com...
                > Wow! Awesome.... Jensen! It is working! But I just wonder... Why that can
                > be
                > working? Because according to the Flash "OO" The object function scope (in
                > function soundComplete) will only contain any property n function for
                > sound...
                > Why the hell if you create new local variable, it will appear in the sound
                > function scope? Can you explain to me a bit more detail? Because I really
                > curious! LOL!
                >
                > David! Long time a go... I try that method... But somehow... The this will
                > return sound reference... Because as far as I see... It quite a bit make
                > sense... Where in sound function if you try to get reference this will
                > return
                > holder of that function which is sound object! So that I get confused and
                > in
                > that function just like isolated... And cannot do anything! But sometime I
                > got
                > annoyed... Altough Macromedia gives us a lot example in almost each
                > function
                > and object... They also almost gives us a simple "example" like... my
                > problem... but the just call statement trace("Oh your object just call
                > onSoundComplete"); lol! Bloody hell! I know that also! LOL! Anyway... But
                > after
                > I think again... Macromedia is the best one for animation design... Sooooo
                > deep
                > sad Macromedia is disappeared... I know Adobe is good company also! I love
                > use
                > their Adobe Photoshop! And I will say again! No substitute for image
                > editor!
                > hahaha!
                >