Actionscript 3.0: Help Linking Document Class To And Audio Slider Class -
so i've been going @ actionscript 3 couple weeks i'm still complete newb. difficulty i've had linking classes document class. example, i'll have nice great class things wonderfully (i insert document class of fla , provide functionality need specific function), when have insert regular class...i guess "subclassing" document class, goes hell.
i know have change variables , instantiate things work , sort of understand that, gets way on head , feel should simple solution if have full working class. seems there's billion things need switch around.
anyways, have specific example i'm hoping explain , walk me through bit. went online , found code slider, spent last few hours editing contain mp3 want, loop it, etc. etc. works great on designated fla...i run document class , pops designed audio slider changes volume, loops , everything. want add slider simple game i've been working on, have no idea start or do. i'll keep simple though.
say have blank document class , audio slider class. when run game, runs document class of course, , there, want run audio slider class directly. think if solve able implement game. here blank document class , audio slider class! help!
what i've tried
i attempted create public variables in document class sprite , slider, create new sprite/slider once document class runs. thought on right track, started looking going have variables in audio slider class. thought...well why can't run volume() in document class? still confusing me little why doesn't work, doesn't.
blank document class
package { import flash.display.movieclip; import flash.display.sprite; public class asdocumentclass extends movieclip { public function asdocumentclass() { } } }
and here audio slider 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=stage.stagewidth/2; public var ypos:number=stage.stageheight/2; 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() { channel=req.play(); channel.addeventlistener( event.sound_complete, onbackgroundmusicfinished,false,0,true ); vol=.5; channel.soundtransform=new soundtransform(vol); init(); } /* 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 init():void { sprite = new sprite(); sprite.graphics.beginfill(0x999999); sprite.graphics.drawrect(xpos,ypos,200,5); sprite.graphics.endfill(); addchild(sprite); sprite.x-=sprite.width/2; slider = new sprite(); slider.graphics.beginfill(0xff0000); slider.graphics.drawcircle(xpos,ypos, 20); slider.graphics.endfill(); addchild(slider); slider.addeventlistener(mouseevent.mouse_down, dragslider); stage.addeventlistener(mouseevent.mouse_up, stopslider); boundary=new rectangle(-100,0,200,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=.5+math.round(slider.x)/200; channel.soundtransform=new soundtransform(vol); } public function onbackgroundmusicfinished(event:event):void { channel = req.play(); channel.addeventlistener( event.sound_complete, onbackgroundmusicfinished ); } } }
it looks though volume
class said, complete , self-contained. good, make instantiating new instance of within document class easier.
within document, class, instantiate new class, can following:
var new_volume:volume = new volume(); addchild(new_volume);
it's important note stage
not come scope within volume
class until have added stage within it's parent class (in case, it's parent class document class).
so these 2 lines:
public var xpos:number=stage.stagewidth/2; public var ypos:number=stage.stageheight/2;
don't work, stage undefined there. wait until know stage
defined, can use event.added_to_stage
event listener. can re-write volume
class bit more this:
package { /* imports here */ public class volume extends sprite { /* other vars here */ public var xpos:number; public var ypos:number; public function volume(){ /* other assignments not stage-dependant can go here */ this.addeventlistener(event.added_to_stage, onstage); } private 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 = stage.stagewidth/2; ypos = stage.stageheight/2; /* have reference stage, let's go ahead , create our slider */ init(); }
from there can go on business usual, , alter variable values needed class work within confines of player environment/document class.
Comments
Post a Comment