actionScript3 ENTER_FRAME 事件无效

actionScript3 ENTER_FRAME Event is not working

我正在尝试使用 ENTER_FRAME 事件来播放音频,但代码没有执行处理函数。

public function play_mp3(path:String,path1:String):void {
    var snd:Sound = new Sound();
    snd.load(new URLRequest(path), new SoundLoaderContext());
    channel = snd.play();
    addEventListener(Event.ENTER_FRAME,myFunction);} 
 public function myFunction(e:Event){
  LogFB.info('test1234'); }

您的问题出在 LogFB 中。
试试这个,你会看到函数内部的跟踪...

   var channel : SoundChannel = new SoundChannel();

function play_mp3(path:String,path1:String):void {
    var snd:Sound = new Sound();
    snd.load(new URLRequest(path), new SoundLoaderContext());
    channel = snd.play();
    addEventListener(Event.ENTER_FRAME,myFunction);
} 

function myFunction(e:Event){
    //LogFB.info('test1234'); here is the problem
   // trace("is working!");
    hi();
}

play_mp3('aasa','aaa');

function hi() {
    trace("goooooddddd!"); //is working, I am using Flash CC
}

祝一切顺利!

似乎没有任何错误试试这个

public function play_mp3(path:String,path1:String):void {
 addEventListener(Event.ENTER_FRAME,myFunction);
    var snd:Sound = new Sound();
    snd.load(new URLRequest(path), new SoundLoaderContext());
    channel = snd.play();
   } 
 public function myFunction(e:Event){
  LogFB.info('test1234'); }

也就是说,将 enterframe 事件放在代码中发生的第一件事,以检查它是否至少初始化了它?

您的问题很可能是您发布其代码的 class 不在显示树中。 (它不是显示对象或尚未添加到舞台)。

如果它不在显示树上,那么 ENTER_FRAME 将不会在其上调度。

您可以通过几种方式解决这个问题。

  1. 将对舞台上某物(或舞台本身)的引用传递到 class。然后在该对象上添加 ENTER_FRAME 侦听器。

    var stage:Stage;
    public function MyClass(stage_:Stage){
        stage = stage_;
    }
    
    ....
    stage.addEventListener(Event.ENTER_FRAME, myFunction);
    
  2. 放弃ENTER_FRAME,只使用Timer

    var timer:Timer
    public function play_mp3(path:String,path1:String):void {
        var snd:Sound = new Sound();
        snd.load(new URLRequest(path), new SoundLoaderContext());
        channel = snd.play();
        timer = new Timer(250); //tick every quarter second
        timer.addEventListener(TimerEvent.TIMER, myFunction);
        timer.start();
    } 
    
    public function myFunction(e:Event){
      LogFB.info('test1234');
    }