PBar

Working on an MP3 player for a client, and decided that the similarities between the progression of the songs as they play and as they download, and the volume and all this crap were enough to warrant this class.

It’s really simple to use… you’ll figure it out.


class PBar
{
   private static var count:Number = 0;

   private var id:Number;

   private var bar:MovieClip;
   private var back:MovieClip;
   private var fill:MovieClip;
   private var outline:MovieClip;   

   private var fill_color:Number = 0xFF0000;
   private var back_color:Number = 0xCCCCCC;
   private var outline_color:Number = 0x0;

   private var width:Number = 100;
   private var height:Number = 10;

   private var percent:Number = 0;
   private var target:MovieClip;

   public function PBar( target:MovieClip, width:Number, height:Number )
   {
      if ( target == undefined )
      {
         throw new Error("Required Parameter target for object PBar");
      }
      else
      {
         this.target = target;
      }
      if (arguments[2] != undefined)
      {
         this.width = width;
         this.height = height;
      }
      draw();

      id = PBar.count++;
   }

   public function setPercent( percent:Number ):Void
   {
      percent = Math.max( percent, 0 );
      percent = Math.min( percent, 100 );

      this.percent = percent; 

      fill._width = width * (percent/100);
   }
   public function getPercent() : Number
   {
      return percent;
   }

   public function setPosition( x:Number, y:Number ):Void
   {
      bar._x = x;
      bar._y = y;
   }

   public function setStyle( fillColor:Number, outlineColor:Number, backColor:Number ) : Void
   {
      if (arguments.length == 3)
      {
         fill_color = fillColor;
         outline_color = outlineColor;
         back_color = backColor;

         draw();
      }
   }

   private function draw():Void
   {
      if (bar == undefined) 
      {
         bar = target.createEmptyMovieClip("pbar" + id, target.getNextHighestDepth()); 
         back = bar.createEmptyMovieClip("back", 0);
         fill = bar.createEmptyMovieClip("fill", 1);
         outline = bar.createEmptyMovieClip("outline", 2);
      }

      var percentWidth:Number = Math.max(width * (percent/100), 1);

      fill.beginFill(fill_color);
      fill.lineTo(0, height); 
      fill.lineTo(1, height);
      fill.lineTo(1, 0);
      fill.lineTo(0, 0);
      fill.endFill();

      fill._width = percentWidth;

      back.beginFill(back_color);
      back.lineTo(0, height); 
      back.lineTo(width, height);
      back.lineTo(width, 0);
      back.lineTo(0, 0);
      back.endFill(); 
      
      outline.lineStyle( 0, outline_color, 100 );
      outline.lineTo(0, height);
      outline.lineTo(width, height);
      outline.lineTo(width, 0);
      outline.lineTo(0, 0);
   }
}

Here’s the code in the fla


var pbar = new PBar( _root, 200, 10);

pbar.setPercent( 50 );
pbar.setStyle( 0x0000FF, 0x0, 0xCCCCCC );
pbar.setPosition( 100, 100 );

trace( pbar.getPercent() );

Take Care
Michael

Because Michael’s a big dork, he asked me to post this sample swf of a sound using the progress bar to show how much of the mp3 is loaded. You may have to allow the swf to access the internet to access the .mp3 when it prompts you to change your security settings.

Here’s the amended code for the .fla:
[AS]
Stage.scaleMode = “noScale”;

var pbar = new PBar( _root, 200, 10);
var song:Sound = new Sound();

pbar.setPercent( 1 );
pbar.setStyle( 0x0000FF, 0x0, 0xCCCCCC );
pbar.setPosition( 100, 100 );

song.loadSound(“http://www.smashbros.com/en/music/mp3/main_theme.mp3”, true);

function onEnterFrame(){
pbar.setPercent(song.getBytesLoaded()/song.getBytesTotal()*100);
}
song.onLoad = function(success:Boolean):Void{
if(success){
delete _root.onEnterFrame;
}
}
[/AS]
[FONT=Courier New][/FONT]
[FONT=Courier New][LEFT][COLOR=#000000]
[/COLOR] [/LEFT]
[/FONT]

I am a big dork :frowning: lol…

His examples shows the progress of the sound file DOWNLOADING, not the progress of it playing. So don’t get confused.

Take Care
Michael

Once its loaded you have to set it to 100% full of blue or it wont work when the page is refreshed. Just an idea it looks only partially full.

Great job i will probably use it.

That’s honestly only the second time I’ve seen someone implement Error - it’s actually a pretty useful feature. Anyways if I were you I’d extend MovieClip and create a component-esque class out of it. Other than that nice work :thumb:

There’s no reason to extend movieclip because it’s all generated dynamically, I use MTASC so the idea is to have no assets, but in any case there is no need for it.

The ideal thing would’ve been to write my own error… and then thrown that, but this will work for now. I’ll get more detailed when I need to.

Thanks for the feedback

Take care
Michael

But for an essentially display oriented object it makes sense to extend MovieClip - dynamically created or not. Ideally you would just drag an instance to the stage and it will take care of the instantiation for you.

And extending Error would be better but it’s best saved for many classes which are in common or for generic errors (such as type errors).

I’m not saying this to just be argumentive, but I don’t understand why you think that extending MovieClip would be a better option. There is no functionality in the MovieClip class that is needed, dragging an instance of this onto the stage is not what this was made for, it’s made to be a quick asset free solution.

The reason you’d extend Error is so that the error can be handled specifically… extending error to make a specific error does nothing more than add granularity to the error. Whether or not the error is used with many classes isn’t really a big reason of whether to use the error or not.

Again not trying to start an argument, just bringing up my side of this.

Take Care

Michael

Thanks again for the feedback :stuck_out_tongue:

Extending MovieClip would give you the same functionality that creating an empty movie clip would. But when you say this is supposed to be an asset free solution I can see what you mean and the advantages of doing it this way.

I was also mainly trying to get at that Error’s should be commonly used before they deserve their own class. Error’s used by just one function have no point in having there own class but Error’s that could pertain to various uses would - something like a ParameterRequiredError which could be used often. Like you said, it only adds granularity. At least I hope I’m interpreting what you are saying right since it seems you know what you’re talking about better than I do :ko:

And I understand you’re not trying to be just argumentative and I hope you understand that I’m not either - I find that I learn best in discussions with differing opinions. And I’m also not trying to diminish your work either, it is, as I’ve said earlier, very useful :mountie:

lol, I completely understand. People get crazy alot so I feel like I have to justify why I’m saying things :P. I agree with you in learning best from discussion. And we are in the same place as far as the Error discussion goes.

Whew, now that it’s all cleared up. haha.

Take Care
Thanks again
Michael