12 Replies Latest reply on Oct 31, 2006 6:19 AM by Miran Loncaric

    Problem with getting the values out of class

    Miran Loncaric
      HI

      The problem is that I cannot get the value from class and then used it in textfield.

      I'm getting different values from XML file and one of them is name of current song.

      It works fine when I trace it inside of for loop which is inside xml.onLoad function
      but when I want to trace it outside of that xml.onLoad function it traces me undefined and the same result is in any other function...

      I don't know where is the problem. I'm sure it's my fault because this is my first class and I don't know much of it right now.

      If you have any idea what am I doing wrong please tell me.

      Thanks in advance

      This is the current code of that class:
        • 3. Re: Problem with getting the values out of class
          Miran Loncaric Level 1
          Thanks for reply!

          Here is XML file:
          • 4. Re: Problem with getting the values out of class
            Miran Loncaric Level 1
            Sample code for using it:
            • 5. Re: Problem with getting the values out of class
              Peter Lorent Level 2
              Put a trace on _index in the progressStatus() function:
              function progressStatus(pBar:MovieClip, scrub_mc:MovieClip, posBar:MovieClip, ozadje_mc:MovieClip) {
              _loaded = _sound.getBytesLoaded()/_sound.getBytesTotal();
              pBar._width = _loaded*ozadje_mc._width;
              scrub_mc._x = (_sound.position/_sound.duration)*pBar._width;
              posBar._width = scrub_mc._x;
              trace(_index);
              }
              What's the result?
              • 6. Re: Problem with getting the values out of class
                Miran Loncaric Level 1
                I get undefined... I've tried that before and many other variables...but all what I get is undefined, exept in playSong function loadSound works fine...
                This is my first class so it's big possibility that the problem is in bad formed class, and because of that I don't know much about scopes and similar things...

                Thanks for reply

                mloncaric
                • 7. Re: Problem with getting the values out of class
                  Peter Lorent Level 2
                  Well, I can't run the complete code (just the xml part) but it looks like a scope problem. I can't see from what scope the function progressStatus is running. Try using the local reference to the class (similar to the usage in loadXML) --- var root=this; and try calling class members using that reference. Otherwise you can zip ALL files and post a link to the zip so I can run the code here.
                  • 8. Re: Problem with getting the values out of class
                    Miran Loncaric Level 1
                    Thanks for helping me!

                    Here is link to files(without of mp3s for keeping the small size of rar file)
                    soundPlayer.rar
                    • 9. Re: Problem with getting the values out of class
                      Peter Lorent Level 2
                      Cannot download a rar. Please use zip.
                      • 11. Re: Problem with getting the values out of class
                        Peter Lorent Level 2
                        See attached code - lines 24/25 and 167/168

                        • 12. Re: Problem with getting the values out of class
                          Miran Loncaric Level 1
                          Thanks for all your help!

                          This is my updated code:
                          In main movie:

                          import com.mloncaric.*;
                          //
                          var player:soundPlayer = new soundPlayer();
                          var updateInterval:Number = new Number();
                          //
                          //
                          player.loadXML("soundPlayer.xml");
                          player.playpause(play_btn, pause_btn);
                          //
                          play_btn.onRollOver = function() {
                          player.RollOver(play_btn);
                          };
                          play_btn.onRollOut = function() {
                          player.RollOut(play_btn);
                          };
                          play_btn.onRelease = function() {
                          player.playSong(play_btn, pause_btn);
                          updateInterval = setInterval(updateFunction, 100);
                          };
                          //
                          pause_btn.onRollOver = function() {
                          player.RollOver(pause_btn);
                          };
                          pause_btn.onRollOut = function() {
                          player.RollOut(pause_btn);
                          };
                          pause_btn.onRelease = function() {
                          player.pauseSong(play_btn, pause_btn);
                          };
                          //
                          stop_btn.onRollOver = function() {
                          player.RollOver(stop_btn);
                          };
                          stop_btn.onRollOut = function() {
                          player.RollOut(stop_btn);
                          };
                          stop_btn.onRelease = function() {
                          player.stopSong(play_btn, pause_btn);
                          clearInterval(updateInterval);
                          };
                          //
                          next_btn.onRollOver = function() {
                          player.RollOver(next_btn);
                          };
                          next_btn.onRollOut = function() {
                          player.RollOut(next_btn);
                          };
                          next_btn.onRelease = function() {
                          player.nextSong();
                          };
                          //
                          prev_btn.onRollOver = function() {
                          player.RollOver(prev_btn);
                          };
                          prev_btn.onRollOut = function() {
                          player.RollOut(prev_btn);
                          };
                          prev_btn.onRelease = function() {
                          player.prevSong();
                          };
                          function updateFunction() {
                          player.update();
                          status_mc.timer.text = player.getTime;
                          }


                          Class code:

                          import com.mosesSupposes.fuse.*;
                          import mx.utils.Delegate;
                          //
                          class com.mloncaric.soundPlayer {
                          private static var MILISECONDS:Number = 1000;
                          //
                          private var xml:XML;
                          private var urls:Array;
                          private var band_txt:Array;
                          private var song_txt:Array;
                          private var album_txt:Array;
                          private var sound:Sound;
                          private var _index:Number = 0;
                          private var pos:Number;
                          private var vol:Number;
                          private var soundPosition, soundDuration:Number;
                          private var soundLoaded, soundTotal:Number;
                          private var _loaded:Number;
                          //
                          private var isPlaying:Boolean = false;
                          //
                          public function soundPlayer() {
                          ZigoEngine.register(Fuse, PennerEasing);
                          //
                          sound = new Sound();
                          }
                          //
                          public function loadXML(targetXML:String) {
                          xml = new XML();
                          xml.ignoreWhite = true;
                          xml.onLoad = Delegate.create(this, onLoadXML);
                          xml.load(targetXML);
                          }
                          private function onLoadXML(success:Boolean) {
                          urls = new Array();
                          band_txt = new Array();
                          song_txt = new Array();
                          album_txt = new Array();
                          //
                          if (success) {
                          var temp:Array = xml.firstChild.childNodes;
                          //
                          for (var i:Number = 0; i<temp.length; i++) {
                          band_txt.push(temp .attributes._band_txt);
                          song_txt.push(temp
                          .attributes._song_txt);
                          album_txt.push(temp .attributes._album_txt);
                          urls.push(temp
                          .attributes._src);
                          }
                          }
                          }
                          //
                          public function playSong(mc1:MovieClip, mc2:MovieClip) {
                          if (isPlaying == false) {
                          startSound();
                          isPlaying = true;
                          playpause(mc1, mc2, isPlaying);
                          } else {
                          startSound();
                          isPlaying = false;
                          playpause(mc1, mc2, isPlaying);
                          }
                          }
                          public function pauseSong(mc1:MovieClip, mc2:MovieClip) {
                          //
                          if (isPlaying == true) {
                          pos = sound.position/MILISECONDS;
                          sound.stop();
                          isPlaying = false;
                          } else {
                          sound.start(pos, 100);
                          isPlaying = true;
                          }
                          }
                          public function stopSong(mc1:MovieClip, mc2:MovieClip) {
                          //
                          pos = 0;
                          sound.stop();
                          if (isPlaying) {
                          isPlaying = false;
                          playpause(mc1, mc2, isPlaying);
                          } else {
                          playpause(mc1, mc2, isPlaying);
                          }
                          }
                          public function nextSong() {
                          if (_index<urls.length-1) {
                          _index++;
                          pos = 0;
                          //
                          if (isPlaying == true) {
                          startSound();
                          }
                          } else {
                          _index = 0;
                          pos = 0;
                          //
                          if (isPlaying == true) {
                          startSound();
                          }
                          }
                          }
                          public function prevSong() {
                          if (_index>0) {
                          _index--;
                          pos = 0;
                          //
                          if (isPlaying == true) {
                          startSound();
                          }
                          } else {
                          _index = urls.length-1;
                          pos = 0;
                          //
                          if (isPlaying == true) {
                          startSound();
                          }
                          }
                          }
                          public function soundVolume() {
                          //
                          sound.setVolume(vol);
                          }
                          public function playpause(mc1:MovieClip, mc2:MovieClip, xy:Boolean) {
                          if (xy == true) {
                          mc1._visible = false;
                          mc2._visible = true;
                          } else {
                          mc2._visible = false;
                          mc1._visible = true;
                          }
                          }
                          public function RollOver(target_mc:MovieClip) {
                          _animation(target_mc, 100);
                          }
                          public function RollOut(target_mc:MovieClip) {
                          _animation(target_mc, 50);
                          }
                          public function get getTime():String {
                          //
                          var timeCurrent:Number = Math.round(soundPosition);
                          var timeTotal:Number = Math.round(soundDuration);
                          var minutesCurrent:Number = Math.round(timeCurrent/60);
                          var minutesTotal:Number = Math.round(timeTotal/60);
                          var secondsCurrent:Number = Math.round(timeCurrent%60);
                          var secondsTotal:Number = Math.round(timeTotal%60);
                          var ct:String = ((minutesCurrent<10) ? "0" : "")+minutesCurrent+":"+((secondsCurrent<10) ? "0" : "")+secondsCurrent;
                          var tt:String = ((minutesTotal<10) ? "0" : "")+minutesTotal+":"+((secondsTotal<10) ? "0" : "")+secondsTotal;
                          //
                          return ct+"/"+tt;
                          }
                          public function progressStatus(pBar:MovieClip, scrub_mc:MovieClip, posBar:MovieClip, ozadje_mc:MovieClip) {
                          //
                          _loaded = soundLoaded/soundTotal;
                          pBar._width = _loaded*ozadje_mc._width;
                          scrub_mc._x = (soundPosition/soundDuration)*pBar._width;
                          posBar._width = scrub_mc._x;
                          }
                          private function _animation(target_mc:MovieClip, alpha:Number) {
                          var f:Fuse = new Fuse();
                          //
                          f.push({target:target_mc, _alpha:alpha, seconds:0.5, ease:"easeOutQuad"});
                          f.start();
                          }
                          private function startSound():Void {
                          sound.start(pos, 100);
                          sound.loadSound(urls[_index], true);
                          }
                          public function update() {
                          soundPosition = sound.position/MILISECONDS;
                          soundDuration = sound.duration/MILISECONDS;
                          soundLoaded = sound.getBytesLoaded();
                          soundTotal = sound.getBytesTotal();
                          }
                          }


                          XML file is the same...

                          This code will be updated soon, but for now it works fine:D

                          Thank you very much man for all your time you wasted for me, and help.

                          mloncaric