AS3 Volume Slider


/ Published in: ActionScript 3
Save to your folder(s)

Simple yet effective way to create a horizontal slider to controll the volume of your sound channel


Copy this code and paste it in your HTML
  1. var maxMove:Number =50;
  2.  
  3. var bounds:Rectangle = new Rectangle(this.volumeSlider.x, this.volumeSlider.y, -maxMove, 0);
  4. var scrolling:Boolean = false;
  5.  
  6. function startScroll (e:Event):void {
  7. scrolling = true;
  8. this.volumeSlider.startDrag (false,bounds);
  9. }
  10.  
  11. function stopScroll (e:Event):void {
  12. scrolling = false;
  13. this.volumeSlider.stopDrag ();
  14. }
  15.  
  16. function enterHandler (e:Event):void {
  17. if (scrolling == true) {
  18. MovieClip(this.root).setVolume(1-((bounds.x-this.volumeSlider.x)/maxMove));
  19. }
  20. }
  21.  
  22. function setVolume(vol:Number){
  23. var mod:SoundTransform = new SoundTransform();
  24. mod.volume = vol;
  25. mp3Channel.soundTransform = mod; //apply to yout mp3Channel
  26. }
  27.  
  28. addEventListener (Event.ENTER_FRAME, enterHandler);
  29.  
  30. this.volumeSlider.addEventListener (MouseEvent.MOUSE_DOWN, startScroll);
  31. stage.addEventListener (MouseEvent.MOUSE_UP, stopScroll);

URL: http://www.project13.pl

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.