Threadを覚えましょう。
Threadのお勉強です。
非同期処理を実装するのにProgをimportしないといけないので、
それに代わるカスタムクラスを作るためのお勉強。
と思ったけど結構壁高い気がしてきた。。。
もうちょい他の勉強してからチャレンジ。
負荷がどうとかはこの際考えない。
♥0 |
Line 93 |
Modified 2010-09-14 01:31:07 |
MIT License
archived:2017-03-20 07:47:04
ActionScript3 source code
/**
* Copyright fakestar0826 ( http://wonderfl.net/user/fakestar0826 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/1yPY
*/
package {
import flash.text.TextField;
import flash.events.Event;
import flash.display.Sprite;
import org.libspark.thread.EnterFrameThreadExecutor;
import org.libspark.thread.Thread;
public class FlashTest extends Sprite {
private var _thread:MyThread;
private var _interruptThread:Thread;
private var _text:TextField;
public function FlashTest() {
// write as3 code here..
_text = new TextField();
stage.addChild(_text);
_text.x = 400;
_text.y = 400;
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
if(!Thread.isReady)
{
Thread.initialize(new EnterFrameThreadExecutor());
}
_thread = new MyThread(this, stage);
_interruptThread = new AnotherThread(_thread);
threadStart();
}
private function threadStart():void
{
_thread.start();
_interruptThread.start();
}
private function enterFrameHandler(e:Event):void
{
//_text.text = ThreadState.state;
}
}
}
import flash.display.Stage;
import flash.events.MouseEvent;
//ふむふむ。各スレッドは完全に独立しているのね。
import flash.display.MovieClip;
import flash.display.Sprite;
import org.libspark.thread.Thread;
class MyThread extends Thread
{
private var _targetShape:Sprite;
private var _stage:Stage;
public function MyThread(threadTarget:Sprite, stage:Stage)
{
_targetShape = threadTarget;
_stage = stage;
}
override protected function run():void
{
sleep(3000);
//割り込まれると↓↓この処理を走らせる。
//interrupted(nextLine);
if(checkInterrupted())
{
//ここで割り込まれると、nextの処理が実行されずに
//finalizeが実行されている。
return;
//↑↑returnはかかなくても一緒・・・?
}
//なるほど、↓↓こっちが先に処理される。
_targetShape.graphics.lineStyle(5, 0xFF0000);
_targetShape.graphics.lineTo(100, 100);
next(nextLine);
}
private function nextLine():void
{
_targetShape.graphics.lineStyle(10, 0x00FF00);
_targetShape.graphics.lineTo(200, 200);
}
override protected function finalize():void
{
event(_stage, MouseEvent.CLICK, clickHandler);
_targetShape.graphics.beginFill(0x0000FF);
_targetShape.graphics.drawRect(200, 200, 200, 100);
_targetShape.graphics.endFill();
}
private function clickHandler(e:MouseEvent):void
{
_targetShape.graphics.lineTo(0,400);
}
}
class AnotherThread extends Thread
{
private var _firstThread:MyThread;
public function AnotherThread(firstThread:MyThread):void
{
_firstThread = firstThread;
}
override protected function run():void
{
sleep(1500);
next(interruptMethod);
}
private function interruptMethod():void
{
_firstThread.interrupt();
}
}