在flash对象中调用函数只能在内部工作

时间:2011-10-14 14:05:09

标签: flash actionscript-3 oop

我正在使用一个软件包来播放Flash中的.wav文件。我可以让wav回放,但是,我不能做的是从按钮触发它们。我怀疑这是因为我误解了这个关键字。以下是一个例子。

你可以看到,当我直接调用playBackTest()时,它可以工作;当我从按钮调用此对象时,它不会。

非常感谢任何建议。

package  
 {  
    import flash.display.Sprite;  
    import flash.net.URLLoader;  
    import flash.net.URLRequest;  
    import flash.events.Event;  
    import flash.utils.ByteArray;  
    import org.as3wavsound.WavSound;  
    import org.as3wavsound.WavSoundChannel;
    import org.as3wavsound.WavSoundPlayer;
    import flash.display.*; 
    import flash.text.TextField;
    import flash.events.ProgressEvent;

     public final class PlaySound extends MovieClip  
     {  
        var tts:WavSound;
        var soundUrl:String;
        var soundType:String;
        var statusTextField:TextField  = new TextField();
        var WavChannel:WavSoundChannel; 
        var statusMC:MovieClip;
        var totalRemainingLoad:int = 0;

        public function PlaySound(soundUrl:String, repeats:String, volumeLevel:Number, statusMC):void {  

            //set variables of this object when it's initiated 
            this.soundUrl = soundUrl;
            this.statusMC = statusMC;
            loadWav();      
        }

        public function loadWav() { 
            var urlRequest:URLRequest = new URLRequest(soundUrl); 
            var wav:URLLoader = new URLLoader();  
            wav.dataFormat = 'binary';  
            wav.load(urlRequest);
            wav.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            this.statusMC.addChild(statusTextField);            
            wav.addEventListener(Event.COMPLETE, makeWav);  
        }

        private function progressHandler(event:ProgressEvent):void {
            var loadTime:Number = event.bytesLoaded / event.bytesTotal;
            var LoadPercent:uint = Math.round(100 * loadTime);
            statusTextField.text =  LoadPercent + ' % loaded ';
        }       

        public function makeWav(e:Event) {  
            this.tts = new WavSound(e.target.data as ByteArray);
            playBackTest();
        }

        public function playBackTest() {
            trace('This makes audio play'); 
            this.tts.play(); 
        }       

        public function playBack() {
            trace('This does not if called from outside with a button press'); 
            this.tts.play(); 
        }   


    }  
}  

1 个答案:

答案 0 :(得分:0)

public function PlaySound():void 
{  
     if(stage) init();
     else      addEventListener(Event.ADDED_TO_STAGE, init);    
}

private function init(e:Event=null):void
{
     removeEventListener(Event.ADDED_TO_STAGE, init);
     loadWav();
}

您的类是继承MovieClip,因此PlaySoundClass应该是一个添加的舞台。 如果你的班级没有完美地添加到ToStage。不可用的课程。

你的内部var更改为Public var或getter / setter方法。因为必须进入。

并按照代码访问您的课程。

var mySound:PlaySound = new PlaySound(); 
mySound.soundUrl = "your URL"; 
mySound.repeats = "your Repeats"; 
mySound.volumeLevel = volumeLevel; 
mySound.statusMc= your MovieClip;