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