スレッドテスト
♥0 |
Line 107 |
Modified 2014-05-19 09:22:52 |
MIT License
archived:2017-03-30 02:41:13
ActionScript3 source code
/**
* Copyright tepe ( http://wonderfl.net/user/tepe )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/k79k
*/
//スレッドのテスト
package {
import flash.display.Sprite;
import flash.text.*;
import flash.events.*;
public class FlashTest extends Sprite {
private var t1:TextField;
private var t2:TextField;
public function FlashTest() {
var th:testFunc = new testFunc();
addChild(th.t1);
addChild(th.t2);
}
}
}
//======= Thread ==========================================================================
import flash.utils.*;
import flash.events.*;
class Thread extends Timer{
static public var SPAN:Number = 1000/30;
static public var RATE:Number = 0.5;
public var end:Boolean = false;
public var currentLoop:Loop = null;
static private var NUM:int = 0;
private var _limit:Number = 5;
private var _time:int = 0;
private var _added:Boolean;
private var currentLoops:Vector.<Loop>;
private var loops:Vector.<Loop> = new Vector.<Loop>();
public function Thread(){
super(SPAN); start(); NUM++;
addEventListener( "timer", onFrame );
}
//loop(実行関数,コンプリートイベント)
public function loop( f:Function, onComplete:Function = null):void{
var loop:Loop = new Loop( f, currentLoop );
if( currentLoop == null ) loops.push( loop );
else{
currentLoop.loops.push( loop );
_added = true;
}
if( onComplete != null ){
loop = new Loop( onComplete, currentLoop );
if( currentLoop == null ) loops.push( loop );
else currentLoop.loops.push( loop );
}
}
public function remove():void{
NUM--; stop();
end = true;
removeEventListener( "timer", onFrame );
}
private function onFrame(e:Event):void{
_time = getTimer();
_limit = (SPAN * RATE) / NUM;
all: while(true){
if( currentLoop == null ){
currentLoops = loops;
if( loops.length == 0 ){ remove(); break; }
while( currentLoops[0].loops.length != 0 ){
currentLoops = currentLoops[0].loops;
}
currentLoop = currentLoops[0];
}
do{
if( _limit < (getTimer() - _time)){ break all; }
if( _added ){ _added = false; currentLoop = null; continue all; }
}while( currentLoop.func() )
currentLoop = null;
currentLoops.reverse();
currentLoops.pop();
currentLoops.reverse();
}
}
}
//====== Loop ============================================================================
class Loop extends Object{
public var func:Function;
public var parent:Loop;
public var loops:Vector.<Loop> = new Vector.<Loop>();
function Loop( f:Function, p:Loop = null){
this.func = f;
this.parent = p;
}
}
//=======================================================================================
import flash.display.*;
import flash.text.*;
class testFunc extends Thread{
public var t1:TextField;
public var t2:TextField;
public function testFunc() {
// write as3 code here..
t1=new TextField();
t2=new TextField();
//stage.addChild(t1);
//stage.addChild(t2);
t2.x=200;
t1.text="test1";
t2.text="test2";
loop(f2,f3);
}
private var cnt:int=0;
private function f2():Boolean{
t1.text = cnt.toString();
cnt++;
if(cnt>5000)return false;
return true;
}
private var cnt2:int=0;
private function f3():void {
cnt2++;
t2.text = cnt2.toString();
}
}