Actionscript 3, adding an audio slider to my game -


so i've been struggling trying add audio slider game last couple days , i've had it, so, let me preface saying have tried extremely hard , love understand solution, @ point want solution, more specific can better.

i've thought of number of approaches, actionscript skills have been faulty pull of them off. let me explain setup bit. have functional class audio slider. plays music, slides, perfect. now, i'm sticking in document class. thought easiest call function right @ beginning (as title screen shows up), though don't want music playing right @ title screen. thought call it, not play music yet. have called document class. now, want able click on options button , addchild audio slider. have accomplished , works. finally....the hard part. want click play game button , have music start playing while being linked options slider has possibility of being moved. really...just standard audio slider.

i'm stuck on linking options screen audio slider music playing in game. thought call volume() function (my audio slider), , once playscreen starts up, call again , run method of volume() class within playscreen class. seems lead problems though...and lose it. calling volume() class again, totally reinstating audio? mean i'm erasing have done audio slider in options panel? seems be...because starts playing @ default value.

so here's detailed code of i'm doing. thinking of using navigation events, said, i'm not greatest flash programmer yet , when tried using them before ran trouble. here main concerns in code.

1) calling new_volume in playscreen class (called simplemenumain). wipe out i've done in document class when called volume()?

2) towards end of volume() class, made stopmusic function. whenever call document class, works if have not yet pressed play game, further leading me believe i'm erasing whatever in options panel before pressing play game.

3) should using navigation events (custom events)? if so, how do it? understand how use them in general (i've used 5 in game), of navigation events go document class, i'm guessing there's subtlety them i'm not quite understanding. have custom events class..i'll post well.

thank =) has been killing me. wittle down these classes relavent info.

document class

package  {     import flash.display.movieclip;     import flash.events.event;     import flash.events.progressevent;      public class smgdocclass extends movieclip      {         public var playscreen:simplemenumain;         public var titlescreen:titlescreen;         public var gameover:gameover;         public var loadingprogress:loadingprogress;         public var optionscreen:optionscreen;         public var new_volume:volume;          public function smgdocclass()          {             loadingprogress = new loadingprogress();             loadingprogress.x = 0;             loadingprogress.y = 0;             addchild( loadingprogress );             loaderinfo.addeventlistener( event.complete, oncompletelydownloaded,false,0,true );             loaderinfo.addeventlistener( progressevent.progress, onprogressmade,false,0,true );         }          public function showtitlescreen():void         {             titlescreen = new titlescreen();             titlescreen.addeventlistener(navigationevent.start,onrequeststart,false,0,true);             titlescreen.addeventlistener(navigationevent.options,onrequestoptions,false,0,true);             titlescreen.x = 0;             titlescreen.y = 0;             addchild(titlescreen);             removechild(loadingprogress);             new_volume = new volume();             new_volume.stopmusic();              stage.focus = playscreen;             stage.stagefocusrect = false;         }          public function onstickman1death(stickman1event:stickman1event):void         {             var finalscore:number = playscreen.getfinalscore();             var finalclocktime:number = playscreen.getfinalclocktime();              gameover = new gameover();             gameover.addeventlistener(navigationevent.restart,onrequestrestart,false,0,true);             gameover.addeventlistener(navigationevent.mainmenu,onrequestmainmenu,false,0,true);             gameover.x = 5;             gameover.y = 6;             gameover.setfinalscore( finalscore );             gameover.setfinalclocktime( finalclocktime );             addchild(gameover);             new_volume.stopmusic();              removechild(playscreen);             playscreen = null;          }          public function onrequeststart( navigationevent:navigationevent ):void         {         playscreen = new simplemenumain();         playscreen.addeventlistener( stickman1event.dead, onstickman1death,false,0,true );         playscreen.x = 0;         playscreen.y = 0;         addchild( playscreen );          dispatchevent(new stickman1event(stickman1event.dead));          removechild(titlescreen);         titlescreen = null;          stage.focus = playscreen;         stage.stagefocusrect = false;         }          public function restartgame():void         {             playscreen = new simplemenumain();             playscreen.addeventlistener(stickman1event.dead, onstickman1death,false,0,true);             playscreen.x = 0;             playscreen.y = 0;             addchild(playscreen);              removechild(gameover);             gameover = null;         }           public function onrequestmainmenu( navigationevent:navigationevent):void         {             titlescreen = new titlescreen();             titlescreen.addeventlistener(navigationevent.start,onrequeststart,false,0,true);             titlescreen.addeventlistener(navigationevent.options,onrequestoptions,false,0,true);             titlescreen.x = 0;             titlescreen.y = 0;             while(numchildren > 0)              {                 removechildat(0);             }             addchild(titlescreen);              stage.focus = playscreen;             stage.stagefocusrect = false;          }          public function oncompletelydownloaded( event:event ):void         {             gotoandstop(3);             showtitlescreen();         }          public function onprogressmade( progressevent:progressevent ):void         {                    loadingprogress.setvalue( math.floor( 100 * loaderinfo.bytesloaded / loaderinfo.bytestotal ) );         }          public function onrequestoptions(navigationevent:navigationevent):void         {             optionscreen = new optionscreen();             optionscreen.addeventlistener(navigationevent.mainmenu,onrequestmainmenu,false,0,true);             optionscreen.x = 0;             optionscreen.y = 0;             addchild(optionscreen);              removechild(titlescreen);          }      } } 

play screen class

package  {      import flash.display.movieclip;     import flash.utils.timer;     import flash.events.timerevent;     import flash.ui.mouse;     import flash.events.keyboardevent;     import flash.ui.keyboard;     import flash.events.event;     import flash.media.soundchannel;      public class simplemenumain extends movieclip {          public var gametimer:timer;         public var stickman1:stickman1;         public var new_volume:volume;           public function simplemenumain() {              currentleveldata = new leveldata( 1 );             setbackgroundimage();              new_volume = new volume();              gametimer = new timer(25);             gametimer.addeventlistener(timerevent.timer, ontick, false, 0, true);             gametimer.start();              addeventlistener( event.added_to_stage, onaddtostage );         }          public function onaddtostage(event:event):void         {             stage.addeventlistener(keyboardevent.key_down, onkeypress,false,0,true);             stage.addeventlistener(keyboardevent.key_up, onkeyrelease,false,0,true);         }          public function ontick(timerevent:timerevent):void         {              var stickman1hasbeenhit:boolean = false;             var i:int = army1.length - 1;             var enemy1:enemy1;             while ( > -1 )             {                 enemy1 = army1[i];                 if (enemyspeed == 1)                 {                     enemy1.moveabit(3,-1.5);                 }                 if (enemyspeed == 2)                 {                     enemy1.moveabit(3.6,-1.8);                 }                 if ( pixelperfectcollisiondetection.iscolliding( stickman1, enemy1, this, true ) )                  {                     getfinalscore();                     gametimer.stop();                     bgmsoundchannel.stop();                     stickman1hasbeenhit = true;                 }                 if ( enemy1.y > 400 )                 {                     removechild( enemy1 );                     army1.splice( i, 1 );                 }                 = - 1;             }              var i:int = army2.length - 1;             var enemy2:enemy2;             while ( > -1 )             {                 enemy2 = army2[i];                 if (enemyspeed2 == 1)                 {                     enemy2.moveabit(6,-3);                 }                  if (pixelperfectcollisiondetection.iscolliding(stickman1, enemy2, this, true ) )                  {                     gametimer.stop();                     bgmsoundchannel.stop();                     stickman1hasbeenhit = true;                 }                 if ( enemy2.y > 400 )                 {                     removechild( enemy2 );                     army2.splice( i, 1 );                 }                 = - 1;             }               if (stickman1hasbeenhit)             {                 dispatchevent(new stickman1event(stickman1event.dead));             }         } } 

volume class

package {          import flash.display.sprite;         import flash.display.graphics;         import flash.events.mouseevent;         import flash.events.event;         import flash.net.urlrequest;         import flash.media.sound;         import flash.media.soundchannel;         import flash.media.soundtransform;         import flash.geom.rectangle;          public class volume extends sprite {                  public var snd:sound = new sound();                 public var channel:soundchannel = new soundchannel();                 //urlrequest=new urlrequest("solitude.wav");                 //make sure pass urlrequest audio file on computer.                 public var req:backgroundmusic = new backgroundmusic();                 public var boundary:rectangle;                 public var sprite:sprite;                 public var slider:sprite;                 public var xpos:number;                 public var ypos:number;                 public var vol:number;                  /*                 our request loaded sound object , plays through                 our channel. volume set @ 50% , passed                 transformation our our channels soundtransform property                 (a fancy way of saying volume). init() function called.                   */                  public function volume() {                         this.addeventlistener(event.added_to_stage, onstage,false,0,true);                         startmusic();                 }                  /*                  init function creates , draws rectangle , circle                 stage , centers them based on height ,                 width of stage. in addition, rectangle object                 created 'contain' sliding circle, imaginary box.                 pass -100 x value because added relative                 our sprite. if set x value @ 0, or sprites default x                 value,the boundary stop , start @ slider sprite. change                 -100 0 in rectangle object better idea of use.                  */                  public function onstage(e:event):void                 {                     //we remove doesn't called multiple times                     //as instance added display list tree                     this.removeeventlistener(event.added_to_stage, onstage);                      xpos = 320;                     ypos = 132;                      /* have reference stage, let's go ahead , create our slider */                     init();                 }                  public function init():void {                         sprite = new sprite();                         channel.stop();                         sprite.graphics.beginfill(0x999999);                         sprite.graphics.drawrect(xpos,ypos,100,5);                         sprite.graphics.endfill();                         addchild(sprite);                         sprite.x-=sprite.width/2;                         slider = new sprite();                         slider.graphics.beginfill(0xff0000);                         slider.graphics.drawcircle(xpos+50,ypos, 15);                         slider.graphics.endfill();                         addchild(slider);                         slider.addeventlistener(mouseevent.mouse_down, dragslider);                         stage.addeventlistener(mouseevent.mouse_up, stopslider);                         boundary=new rectangle(-100,0,100,0);                 }                  /*                  dragslider runs when use holds mouse button down.                 startdrag method used on our sprite specify boundary                 our dragging limits. new event handler designed                 change mouse volume subsequenlty called per frame,                 slider.x property determines volume.                  */                  public function dragslider(event:mouseevent):void {                         slider.startdrag(false,boundary);                         slider.removeeventlistener(mouseevent.click, dragslider);                         slider.addeventlistener(event.enter_frame, changevolume);                 }                  /*                  stops dragging , removes event listener save on space. again,                 volume based on sliders current x position,                 being recalculated per frame because used                 enter_frame event.                  */                  public function stopslider(event:mouseevent):void {                         slider.stopdrag();                         slider.removeeventlistener(mouseevent.mouse_up, stopslider);                 }                  /*                  function recalculating vol variable                 based on sliders x position, relative length of                 our rectangle. creates decimal range 0 1, 1                 represents 100% volume , 0 represents mute. exceeding                 100% causes distortion.                  */                  public function changevolume(event:event):void {                         vol=1+math.round(slider.x)/100;                         channel.soundtransform=new soundtransform(vol);                 }                  public function onbackgroundmusicfinished(event:event):void                 {                     channel = req.play();                     channel.addeventlistener( event.sound_complete, onbackgroundmusicfinished );                     vol=1;                     channel.soundtransform=new soundtransform(vol);                 }                  public function startmusic():void                 {                     channel=req.play();                     channel.addeventlistener( event.sound_complete, onbackgroundmusicfinished,false,0,true );                     vol=1;                     channel.soundtransform=new soundtransform(vol);                 }                  public function playmusic():void                 {                     channel = req.play();                 }                  public function stopmusic():void                 {                     channel.stop();                 }          }  } 

navigation event class

package   {     import flash.events.event;     public class navigationevent extends event      {         public static const restart:string = "restart";         public static const start:string = "start";         public static const mainmenu:string = "mainmenu";         public static const options:string = "options";         public static const stopmusic:string = "stopmusic"          public function navigationevent( type:string, bubbles:boolean = false, cancelable:boolean = false )          {              super( type, bubbles, cancelable );          }           public override function clone():event          {              return new navigationevent( type, bubbles, cancelable );         }          public override function tostring():string          {              return formattostring( "navigationevent", "type", "bubbles", "cancelable", "eventphase" );          }     } } 

options screen class

package  {      import flash.display.movieclip;     import flash.display.simplebutton;     import flash.events.mouseevent;     import flash.text.textfield;     import flash.ui.mouse;     import flash.net.sharedobject;     import flash.events.event;       public class optionscreen extends movieclip {          public var mainmenubutton:simplebutton;         private var new_volume:volume;         public var myevent:event;          public function optionscreen() {              mouse.show();             new_volume = new volume();             addchild(new_volume);             mainmenubuttonoptions.addeventlistener(mouseevent.click, onclickmainmenu,false,0,true);         }          public function onclickmainmenu(mouseevent:mouseevent):void         {             dispatchevent( new navigationevent(navigationevent.mainmenu));         }     }  } 

finally....title screen class

package  {     import flash.display.movieclip;     import flash.display.simplebutton;     import flash.events.mouseevent;     import flash.ui.mouse;      public class titlescreen extends movieclip      {         public var startbutton:simplebutton;          public function titlescreen()          {             mouse.show();             startbutton.addeventlistener( mouseevent.click, onclickstart,false,0,true );             titleoptionsbutton.addeventlistener( mouseevent.click, onclickoptions,false,0,true );         }          public function onclickstart( event:mouseevent ):void         {             dispatchevent( new navigationevent( navigationevent.start ) );         }          public function onclickoptions( event:mouseevent ):void         {             dispatchevent( new navigationevent( navigationevent.options ) );         }       } } 

you can change volume on individual soundchannels, or globally on soundmixer class. current implementation former, , defined every time instantiate class.

see adobe's controlling sound volume , panning.

you can set global volume , pan values sounds @ once using soundtransform property of soundmixer class, following example shows:

soundmixer.soundtransform = new soundtransform(1, -1);

you store volume , reference global/static variable.

public static var soundlevel:number = 100;  public function changevolume(event:event):void {     soundlevel = 1+math.round(slider.x)/100;     channel.soundtransform=new soundtransform(soundvolume); } 

this way, slider object changes same variable. of course, you're left needing update n number of soundobjects new soundlevel, why using soundmixer better solution.

  1. no. new instance of volume class doesn't overwrite soundchannel created. however, because volume displayobject, if reference on stage, , starting game takes different frame, old volume object removed displaylist , garbage collected.

  2. definitely sounds you're moving between frames, in case older volume object removed displaylist , gc'ed. comes lot, i'll reiterate: don't use stage frames. there better (less headache inducing) ways of programming need.

  3. your choice of nav events discretional. write nav class handles input events come system, , appropriately call methods. personally, don't flash style events. there's other data want pass methods apart usual variable suspects, thereby leading clutter induced custom events. clear global state tracking, , reference current state generic methods determine response given nav event.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -