0

I have a slider that control the volume of the background music.

In the slider component parameter value: I set it as 5.

When I slide the slider to 2 and navigate to other movie clip. And subsequently return to the slider the value is 5. How can i store the value of 2 instead of showing 5?

import fl.events.SliderEvent;
import flash.media.SoundTransform;

var myTransform = new SoundTransform();

scMC.addEventListener(SliderEvent.THUMB_DRAG, changeVolume);

function changeVolume(event:SliderEvent):void{
MovieClip(this.root).myChannel.soundTransform = myTransform;
myTransform.volume = scMC.value;
volumeText.text = "Volume is " + scMC.value;

}
4

2 に答える 2

0

You'll need to store the value in either a global or static variable. If you don't want to get into custom class files and static variables, an easy way you can do this using timeline code would be the following:

In your root timeline code:

var sliderVal;

In your slider parents timeline code (from your question):

var myTransform:SoundTransform = new SoundTransform();

scMC.addEventListener(SliderEvent.THUMB_DRAG, changeVolume);

if(this.root["sliderVal"]){
    scMC.value = this.root["sliderVal"];
}

function changeVolume(event:SliderEvent):void{
    myTransform.volume = scMC.value;
    this.root["sliderVal"] = scMC.value;
    MovieClip(this.root).myChannel.soundTransform = myTransform;
    volumeText.text = "Volume is " + scMC.value;

} 
于 2012-08-27T17:27:39.957 に答える
0

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.

  1. 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.
  2. 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.
  3. It automatically removes the listener when Flash removes the Slider from the stage, which could prevent memory leaks.
于 2012-08-28T02:18:13.773 に答える