在actionscript3中为声音变换添加变量时出错

时间:2012-11-23 14:09:50

标签: actionscript-3 flash

我试图在点击按钮时调整声音的音量。

  

第86行1120:访问未定义的属性adjustVolume

     

第91行1120:访问未定义的属性adjustVolume

这是代码

package 
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import fl.controls.CheckBox;
import fl.controls.Slider;
import flash.media.SoundTransform;

public class Track extends MovieClip
{
    public var trackSound:Sound;
    public var theChannel:SoundChannel;
    public var songName:String;
    public var currentStep:Number = 0;
    public var checkboxArray:Array;
    //public var adjustVolume:SoundTransform = new SoundTransform();
    /*adjustVolume.volume = 1;
    theChannel.soundTransform = adjustVolume;*/


    // onoff array of 16 false booleans
    public function Track()
    {
        // constructor code

        var adjustVolume:SoundTransform = new SoundTransform();
        adjustVolume.volume = 1;
        theChannel.soundTransform = adjustVolume;


        //trace( this.parent.name+"."+ this.name + " track created "  );
        singlePlay.addEventListener(flash.events.MouseEvent.CLICK, handlePlayClick);

        trackVolDown.addEventListener(flash.events.MouseEvent.CLICK, trackVolumeHandler);
        trackVolUp.addEventListener(flash.events.MouseEvent.CLICK, trackVolumeHandler);

        c1.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);
        c2.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);
        c3.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);
        c4.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);
        c5.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);
        c6.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);
        c7.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);
        c8.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);
        c9.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);
        c10.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);
        c11.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);
        c12.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);
        c13.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);
        c14.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);
        c15.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);
        c16.addEventListener(flash.events.MouseEvent.CLICK, handleStepClick);

        checkboxArray = new Array(c1,c2, c3, c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16);
        //trace (checkboxArray[0].selected);            
    }
    private function handlePlayClick(e:Event):void
    {
        //trace( this.parent.name+"."+ this.name +" Play clicked");
        this.playSound();
    }       
    public function setSoundName(theName:String)
    {
        this.songName = theName;
        trackSound = new Sound();
        var req:URLRequest = new URLRequest(this.songName);
        trackSound.addEventListener(Event.COMPLETE, onSoundLoaded);
        trackSound.load(req);
    }

    private function onSoundLoaded(e:Event):void
    {
        //trace(this.songName + " loaded into sequencer");
    }

    public function playSound( currentStep:Number = -1)
    {

        if (currentStep == -1)
        {
            theChannel = trackSound.play();
            theChannel.soundTransform = adjustVolume;//error here
        }
        else if (this.checkboxArray[currentStep].selected)
        {
            theChannel = trackSound.play();
            theChannel.soundTransform = adjustVolume;// and error here
        }
    }
    public function stopSound():void
    {
        theChannel.stop();
    }

    private function handleStepClick(e:Event):void
    {
        trace( this.name+'.'+(e.target as CheckBox).name);

    }   
    private function trackVolumeHandler(e:Event):void
    {
        //trace( this.parent.name+"."+ this.name +" Play clicked");
        this.soundTransform;
    }       

}
}

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

TL;博士

使用构造函数初始化字段。

详细

替换以下行:

var adjustVolume:SoundTransform = new SoundTransform();
adjustVolume.volume = 1;
theChannel.soundTransform = adjustVolume;

由:

private var adjustVolume:SoundTransform;
public function Track()
{
    adjustVolume = new SoundTransform();
    adjustVolume.volume = 1;
    theChannel.soundTransform = adjustVolume;
}

答案 1 :(得分:0)

替换

public var theChannel:SoundChannel;

public var theChannel:SoundChannel = new SoundChannel;

现在可以使用了