Actionscript 3中的伪线程(对于Flash)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Actionscript 3中的伪线程(对于Flash)相关的知识,希望对你有一定的参考价值。

  1. ackage be.boulevart.threading { import flash.display.Sprite; import flash.display.Stage; import flash.events.Event; import flash.events.EventDispatcher; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.utils.getTimer; public class PseudoThread extends EventDispatcher { // number of milliseconds we think it takes to render the screen public var RENDER_DEDUCTION : int = 20; private var fn : Function; private var obj : Object; private var thread : Sprite; private var start : Number; private var due : Number; private var sm : Sprite private var mouseEvent : Boolean; private var keyEvent : Boolean; public function PseudoThread(yourStage : Stage,threadFunction : Function,threadObject : Object = null) { fn = threadFunction; sm = new Sprite() yourStage.addChild(sm) if(obj == null) { obj = threadObject; }else { obj = new Object() } // add high priority listener for ENTER_FRAME sm.stage.addEventListener(Event.ENTER_FRAME , enterFrameHandler , false , 200 , true); sm.stage.addEventListener(MouseEvent.MOUSE_MOVE , mouseMoveHandler , false , 0 , true); sm.stage.addEventListener(KeyboardEvent.KEY_DOWN , keyDownHandler , false , 0 , true); thread = new Sprite(); sm.addChild(thread); thread.addEventListener(Event.RENDER , renderHandler); } private function enterFrameHandler(event : Event) : void { start = getTimer(); var fr : Number = Math.floor(1000 / thread.stage.frameRate); due = start + fr; thread.stage.invalidate(); thread.graphics.clear(); thread.graphics.moveTo(0 , 0); thread.graphics.lineTo(0 , 0); } private function renderHandler(event : Event) : void { if (mouseEvent || keyEvent) due -= RENDER_DEDUCTION; while (getTimer() < due) { if(obj == null) { if (!fn()) { if (!thread.parent) return; sm.stage.removeEventListener(Event.ENTER_FRAME , enterFrameHandler); sm.stage.removeEventListener(MouseEvent.MOUSE_MOVE , mouseMoveHandler); sm.stage.removeEventListener(KeyboardEvent.KEY_DOWN , keyDownHandler); sm.removeChild(thread); thread.removeEventListener(Event.RENDER , renderHandler); thread = null thread = new Sprite() dispatchEvent(new Event("threadComplete")); } }else { if (!fn(obj)) { if (!thread.parent) return; sm.stage.removeEventListener(Event.ENTER_FRAME , enterFrameHandler); sm.stage.removeEventListener(MouseEvent.MOUSE_MOVE , mouseMoveHandler); sm.stage.removeEventListener(KeyboardEvent.KEY_DOWN , keyDownHandler); sm.removeChild(thread); thread = null thread = new Sprite() thread.removeEventListener(Event.RENDER , renderHandler); dispatchEvent(new Event("threadComplete")); } } } mouseEvent = false; keyEvent = false; } private function mouseMoveHandler(event : Event) : void { mouseEvent = true; } private function keyDownHandler(event : Event) : void { keyEvent = true; } } }

以上是关于Actionscript 3中的伪线程(对于Flash)的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Actionscript 3、Flex 4 SDK 和 FlashDevelop 从 .fla 文件访问数据?

我可以在没有源 FLA 文件的情况下更改 SWF 中的 ActionScript 字符串文字吗?

无法将其他 Flash 影片剪辑访问到 Actionscript 3

ActionScript 3.0 类

将带有 AS2 动作和 ActionScript 的 FLA 资源编译为 SWF

Adobe Flash ActionScript 1.0 .fla 文件