Your problem is that you haven't separated your View from your Model (data), so the lifespan of the Model is identical to the lifespan of the View. To allow the Model to survive independently of the View, you need to make it that the View merely displays the value it is given and makes any changes in the value to the outside available for later use.
So, something like:
public class SliderView extends MovieClip {
protected var _slider:Slider;
protected var _sliderValue:Number;
/*Flash will populate this variable for you if you put
a slider on the stage and give it an instance name of "slider."
*/
public function get slider():Slider {
return _slider;
}
public function set slider(value:Slider):void {
if (value != _slider) {
if (_slider) {
_slider.removeEventListener(SliderEvent.CHANGE, onSliderChange);
}
_slider=value;
if (_slider) {
_slider.addEventListener(SliderEvent.CHANGE, onSliderChange);
}
}
}
public function get sliderValue():Number{
return _sliderValue;
}
public function set sliderValue(value:Number):void {
if (value != _sliderValue) {
_sliderValue = value;
if (_slider) {
_slider.value = value;
}
dispatchEvent(new Event('sliderValueChanged'));//anything needing to know about the change is notified immediately
}
}
protected function onSliderChange(e:SliderEvent):void {
sliderValue = Slider(e.target).value;
}
}
Note that this code has several advantages from a maintainability perspective than the timeline-style code you've posted.
- It isn't trying to manage the volume. This means that it will continue to work even if you need to use it somewhere else or the structure of your MC changes. To manage the volume, you'd listen for sliderValueChanged at a higher level and change the volume there. If you set the event to bubbling, you could listen all the way up at the root, where you've stored everything (apparently). That's not very good encapsulation, though.
- It's completely data driven, meaning that if you have previously stored the sliderValue, passing that same value into sliderValue will cause you to see the same picture. Or, you could read it off the SoundTransform. Hopefully you'll encapsulate that better than just dumping that on the root like you have it in your prototype code.
- It automatically removes the listener when Flash removes the Slider from the stage, which could prevent memory leaks.