·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3异步执行的问题

时间:2015-06-12 14:02酷播
[AS3]as3异步执行的问题,as3异步,as3异步执行,舞台上一mc,mc含有多帧,每帧均有且仅有一名为aa的元件

问题:
舞台上一mc,mc含有多帧,每帧均有且仅有一名为aa的元件。

mc.gotoAndStop(1);
mc.aa.visible = false;

此时运行正常。但改成

mc.gotoAndStop(2);
mc.aa.visible = false;

运行提示错误,mc.aa为null。

原因:
MovieClip的帧显示对象的构造和帧代码是异步执行,所以才造成在第二帧代码访问帧上的对象为null的情况。

解决方法:

  1. private function test():void 
  2. mc.gotoAndStop(2); 
  3. mc.addEventListener(Event.FRAME_CONSTRUCTED, _onFrameConstructed); 
  4. private function _onFrameConstructed(e:Event):void 
  5. mc.removeEventListener(Event.FRAME_CONSTRUCTED, _onFrameConstructed); 
  6. mc.aa.visible = false

一帧里的执行顺序:

Event of event type Event.ENTER_FRAME dispatched(发送 Event.ENTER_FRAME 事件)
Constructor code of children MovieClips is executed(执行子MC的构造函数)
Event of event type Event.FRAME_CONSTRUCTED dispatched(发送 Event.FRAME_CONSTRUCTED 事件)
MovieClip frame actions are executed(执行MC的构造函数)
Frame actions of children MovieClips are executed(执行子MC的帧代码)
Event of event type Event.EXIT_FRAME dispatched(发送 Event.EXIT_FRAME 事件)
Event of event type Event.RENDER dispatched(发送 Event.RENDER 事件)

Step 3(第三步):
This caused all sort of issues in Flash Player 9 after issuing a gotoAndStop call to a frame with display list objects that didn’t exist on the frame where the gotoAndStop call was made. Due to the fact that display list children had yet to be instantiated, targeting a display object child would return null the first time an Event.ENTER_FRAME was called after the gotoAndStop.
(当调用gotoAndStop后,一系列显示对象并不存在。这是因为当发送 Event.ENTER_FRAME 事件时,显示对象并没有被实例化,所以此时返回对象为空。而通过侦听 Event.FRAME_CONSTRUCTED 事件,则可在对象实例化后才调用相关语句。)

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器