简体   繁体   中英

actionScript3 ENTER_FRAME Event is not working

I am trying to to use the ENTER_FRAME Event on playing audio ,but the code is not executing the handler function.

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'); }

You're issue is likely that the class whose code you've posted, is not on the display tree. (it's not a display object or hasn't been added to the stage).

If it's not on the display tree, then ENTER_FRAME will not dispatch on it.

You can get around this a few ways.

  1. Pass into the class a reference to something that is on the stage (or the stage itself). Then add the ENTER_FRAME listener on that object.

     var stage:Stage; public function MyClass(stage_:Stage){ stage = stage_; } .... stage.addEventListener(Event.ENTER_FRAME, myFunction); 
  2. Forgo ENTER_FRAME, and just use a 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'); } 

Your problem is in LogFB.
Try this and you will see the trace from inside the function...

   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
}

All the best!

there doesnt seem any error try this one

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'); }

ie put the enterframe event the first thing that hapen in your code to check if at least it initialise it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM