In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

ひよこちゃんで学ぶThread(そうめん)

Threadがいまだにつかみみ切れていません。
ここはこんなふうにした方が!とか、こんな方法もあるよね!とか。
forkしてくださいませ。
Get Adobe Flash player
by ProjectNya 10 Nov 2010
    Embed
/**
 * Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/c4tU
 */

////////////////////////////////////////////////////////////////////////////////
// ひよこちゃんで学ぶThread(そうめん)
////////////////////////////////////////////////////////////////////////////////

package {

    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.URLRequest;
    import flash.system.Security;
    import flash.system.LoaderContext;
    import flash.display.Shape;
    import flash.geom.Matrix;
    import flash.display.GradientType;
    import flash.display.BlendMode;
    import org.libspark.thread.Thread;
    import org.libspark.thread.EnterFrameThreadExecutor;

    [SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]

    public class Main extends Sprite {
        private static var basePath:String = "http://assets.wonderfl.net/images/related_images/";
        private static var sunshinePath:String = "9/9b/9bbe/9bbec77d53bddc5e7a5f2c00abbade6bd641c549";
        private var loader:Loader;
        private static var piyoPath:String = "http://www.project-nya.jp/images/flash/piyo2d.swf";
        private var boxloader:Loader;
        private static var boxPath:String = "http://www.project-nya.jp/images/flash/cardboardbox.swf";
        private var Piyo:Class;
        private var piyo:MovieClip;
        private var CardboardBox:Class;
        private var box:MovieClip;
        private var startBtn:Btn;
        private var box1:MovieClip;
        private var box2:MovieClip;

        public function Main() {
            //Wonderfl.capture_delay(4);
            init();
        }

        private function init():void {
            draw();
            Security.allowDomain("www.project-nya.jp");
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, complete, false, 0, true);
            loader.load(new URLRequest(piyoPath), new LoaderContext(true));
            boxloader = new Loader();
            boxloader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded, false, 0, true);
            boxloader.load(new URLRequest(boxPath), new LoaderContext(true));
            startBtn = new Btn();
            //
            addChild(startBtn);
            startBtn.x = 232;
            startBtn.y = 445;
            startBtn.init({label: "start"});
            startBtn.addEventListener(MouseEvent.CLICK, start, false, 0, true);
            //
            if (!Thread.isReady) Thread.initialize(new EnterFrameThreadExecutor());
        }
        /////////////////////////////////////////////
        //Thread
        /////////////////////////////////////////////
        private function start(evt:MouseEvent):void {
            //box1.progress = 0.5;
            //box2.progress = 0.5;
            box1.paper.y = -35;
            box2.paper.y = -35;
            var thread:MainThread = new MainThread(this, piyo, [box1, box2], reset);
            thread.start();
            startBtn.clicked = true;
            startBtn.removeEventListener(MouseEvent.CLICK, start);
        }
        private function reset():void {
            startBtn.clicked = false;
            startBtn.addEventListener(MouseEvent.CLICK, start, false, 0, true);
        }
        /////////////////////////////////////////////
        //読み込み
        /////////////////////////////////////////////
        private function complete(evt:Event):void {
            evt.target.removeEventListener(Event.COMPLETE, complete);
            var content:MovieClip = MovieClip(evt.target.content);
            //Piyoクラス
            Piyo = MovieClip(content.piyo).constructor;
            setup();
            loader = null;
        }
        private function setup():void {
            //Piyoインスタンス
            piyo = new Piyo();
            addChild(piyo);
            piyo.x = 232;
            piyo.y = 400;
            piyo.scale = 2;
            piyo.mouseChildren = false;
        }
        private function loaded(evt:Event):void {
            evt.target.removeEventListener(Event.COMPLETE, loaded);
            var content:MovieClip = MovieClip(evt.target.content);
            //CardboardBoxクラス
            CardboardBox = MovieClip(content.base).constructor;
            setupBox();
            loader = null;
        }
        private function setupBox():void {
            //CardboardBoxインスタンス
            //
            box1 = new CardboardBox();
            addChild(box1);
            box1.x = 72;
            box1.y = 380;
            //box1.progress = 0.5;
            box1.paper.y = -35;
            box1.scaleX = box1.scaleY = 2;
            box2 = new CardboardBox();
            addChild(box2);
            box2.x = 392;
            box2.y = 380;
            //box2.progress = 0.5;
            box2.paper.y = -35;
            box2.scaleX = box2.scaleY = 2;
        }
        /////////////////////////////////////////////
        //背景
        /////////////////////////////////////////////
        private function draw():void {
            drawSky();
            drawGround();
            drawSun();
        }
        private function drawSky():void {
            var matrix:Matrix = new Matrix();
            matrix.createGradientBox(465, 350, 0.5*Math.PI, 0, 0);
            graphics.beginGradientFill(GradientType.LINEAR, [0x3F68AB, 0x77B2EE], [1, 1], [0, 255], matrix);
            graphics.drawRect(0, 0, 465, 350);
            graphics.endFill();
        }
        private function drawGround():void {
            var matrix:Matrix = new Matrix();
            matrix.createGradientBox(465, 115, 0.5*Math.PI, 0, 350);
            graphics.beginGradientFill(GradientType.LINEAR, [0x99CC33, 0x7EB133], [1, 1], [0, 255], matrix);
            graphics.drawRect(0, 350, 465, 115);
            graphics.endFill();
        }
        private function drawSun():void {
            var matrix:Matrix = new Matrix();
            matrix.createGradientBox(200, 200, 0, -90, -90);
            graphics.beginGradientFill(GradientType.RADIAL, [0xFFFFFF, 0xFFFFFF, 0xFFFFFF], [1, 0.3, 0], [25, 102, 231], matrix);
            graphics.drawCircle(10, 10, 100);
            graphics.endFill();
            var shine:Loader = new Loader();
            addChild(shine);
            shine.alpha = 0.5;
            shine.load(new URLRequest(basePath + sunshinePath), new LoaderContext(true));
        }

    }

}


//////////////////////////////////////////////////
// MainThreadクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.display.MovieClip;
import org.libspark.thread.Thread;
import org.libspark.thread.utils.SerialExecutor;

class MainThread extends Thread {
    private var container:Sprite;
    private var piyo:MovieClip;
    private var box1:MovieClip;
    private var box2:MovieClip;
    private var callback:Function;

    public function MainThread(c:Sprite, p:MovieClip, boxes:Array, cb:Function) {
        container = c;
        piyo = p;
        box1 = boxes[0];
        box2 = boxes[1];
        callback = cb;
    }

    override protected function run():void {
        var threads:SerialExecutor = new SerialExecutor();
        threads.addThread(new MoveThread(piyo, 182, 0.6, "linear", box1));
        threads.addThread(new MoveThread(piyo, 232, 0.6, "linear"));
        threads.addThread(new MoveThread(piyo, 282, 0.6, "linear", box2));
        threads.addThread(new MoveThread(piyo, 232, 0.6, "linear"));
        threads.start();
        threads.join();
    }
    override protected function finalize():void {
        callback();
    }

}


//////////////////////////////////////////////////
// MoveThreadクラス
//////////////////////////////////////////////////

import flash.display.MovieClip;
import org.libspark.thread.Thread;
import org.libspark.thread.threads.tweener.TweenerThread;
import caurina.transitions.Tweener;

class MoveThread extends Thread {
    private var piyo:MovieClip;
    private var targetPos:Number;
    private var time:Number;
    private var transition:String;
    private var box:MovieClip;

    public function MoveThread(p:MovieClip, tp:Number, t:Number, ts:String, b:MovieClip = null) {
        piyo = p;
        targetPos = tp;
        time = t;
        transition = ts;
        box = b;
    }

    override protected function run():void {
        var thread:TweenerThread = new TweenerThread(piyo, {x: targetPos, time: time, transition: transition});
        thread.start();
        thread.join();
        next(reach);
    }
    private function reach():void {
        //piyo.swingHead();
        if (box) {
            var thread:TaskThread = new TaskThread(box);
            thread.start();
            thread.join();
        }
    }
    override protected function finalize():void {
    }

}


//////////////////////////////////////////////////
// TaskThreadクラス
//////////////////////////////////////////////////

import flash.display.MovieClip;
import org.libspark.thread.Thread;
import org.libspark.thread.threads.tweener.TweenerThread;
import caurina.transitions.Tweener;

class TaskThread extends Thread {
    private var box:MovieClip;

    public function TaskThread(b:MovieClip) {
        box = b;
    }

    override protected function run():void {
        sleep(500);
        next(open);
    }
    private function open():void {
        //box.open();
        box.gotoAndStop("open");
        var thread:TweenerThread = new TweenerThread(box.paper, {y: -30, time: 2, transition: "linear"});
        thread.start();
        thread.join();
        next(close);
    }
    private function close():void {
        sleep(500);
    }
    override protected function finalize():void {
        //box.close();
        box.gotoAndStop("close");
    }

}


//////////////////////////////////////////////////
// Btnクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.GlowFilter;
import flash.events.MouseEvent;

class Btn extends Sprite {
    public var id:uint;
    private var shade:Shape;
    private var bottom:Shape;
    private var light:Shape;
    private var base:Shape;
    private var txt:TextField;
    private var label:String = "";
    private static var fontType:String = "_ゴシック";
    private var _width:uint = 60;
    private static var _height:uint = 20;
    private static var corner:uint = 5;
    private var type:uint = 1;
    private static var bColor:uint = 0xFFFFFF;
    private static var sColor:uint = 0x000000;
    private static var upColor:uint = 0x666666;
    private static var overColor:uint = 0x333333;
    private static var offColor:uint = 0x999999;
    private static var gColor:uint = 0x0099FF;
    private var blueGlow:GlowFilter;
    private var shadeGlow:GlowFilter;
    private var _clicked:Boolean = false;
    private var _enabled:Boolean = true;

    public function Btn() {
    }

    public function init(option:Object):void {
        if (option.id != undefined) id = option.id;
        if (option.label != undefined) label = option.label;
        if (option.width != undefined) _width = option.width;
        if (option.type != undefined) type = option.type;
        draw();
    }
    private function draw():void {
        switch (type) {
        case 1 :
            bColor = 0xFFFFFF;
            sColor = 0x000000;
            upColor = 0x666666;
            overColor = 0x333333;
            offColor = 0x999999;
            break;
        case 2 :
            bColor = 0x000000;
            sColor = 0xFFFFFF;
            upColor = 0x666666;
            overColor = 0x999999;
            offColor = 0x333333;
            break;
        }
        blueGlow = new GlowFilter(gColor, 0.6, 5, 5, 2, 3, false, true);
        shadeGlow = new GlowFilter(sColor, 0.3, 4, 4, 2, 3, false, true);
        shade = new Shape();
        bottom = new Shape();
        light = new Shape();
        base = new Shape();
        txt = new TextField();
        addChild(shade);
        addChild(bottom);
        addChild(light);
        addChild(base);
        addChild(txt);
        createBase(shade, _width, _height, corner, sColor);
        shade.filters = [shadeGlow];
        createBase(bottom, _width, _height, corner, sColor, 0.3);
        createBase(light, _width, _height, corner, gColor);
        light.filters = [blueGlow];
        createBase(base, _width, _height, corner, bColor);
        txt.x = -_width*0.5;
        txt.y = -_height*0.5;
        txt.width = _width;
        txt.height = _height - 1;
        txt.type = TextFieldType.DYNAMIC;
        txt.selectable = false;
        //txt.embedFonts = true;
        //txt.antiAliasType = AntiAliasType.ADVANCED;
        var tf:TextFormat = new TextFormat();
        tf.font = fontType;
        tf.size = 12;
        tf.align = TextFormatAlign.CENTER;
        txt.defaultTextFormat = tf;
        txt.text = label;
        enabled = true;
        mouseChildren = false;
    }
    private function rollOver(evt:MouseEvent):void {
        _over();
    }
    private function rollOut(evt:MouseEvent):void {
        _up();
    }
    private function press(evt:MouseEvent):void {
        _down();
    }
    private function release(evt:MouseEvent):void {
        _up();
    }
    private function click(evt:MouseEvent):void {
    }
    private function _up():void {
        txt.y = -_height*0.5;
        txt.textColor = upColor;
        base.y = -1;
        light.visible = false;
        light.y = -1;
    }
    private function _over():void {
        txt.y = -_height*0.5;
        txt.textColor = overColor;
        base.y = -1;
        light.visible = true;
        light.y = -1;
    }
    private function _down():void {
        txt.y = -_height*0.5 + 1;
        txt.textColor = overColor;
        base.y = 0;
        light.visible = true;
        light.y = 0;
    }
    private function _off():void {
        txt.y = -_height*0.5 + 1;
        txt.textColor = offColor;
        base.y = 0;
        light.visible = false;
        light.y = 0;
    }
    public function get clicked():Boolean {
        return _clicked;
    }
    public function set clicked(param:Boolean):void {
        _clicked = param;
        enabled = !_clicked;
        if (_clicked) {
            _down();
        } else {
            _up();
        }
    }
    public function get enabled():Boolean {
        return _enabled;
    }
    public function set enabled(param:Boolean):void {
        _enabled = param;
        buttonMode = _enabled;
        mouseEnabled = _enabled;
        useHandCursor = _enabled;
        if (_enabled) {
            _up();
            addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
            addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
            addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
            addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
            addEventListener(MouseEvent.CLICK, click, false, 0, true);
        } else {
            _off();
            removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
            removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
            removeEventListener(MouseEvent.MOUSE_DOWN, press);
            removeEventListener(MouseEvent.MOUSE_UP, release);
            removeEventListener(MouseEvent.CLICK, click);
        }
    }
    private function createBase(target:Shape, w:uint, h:uint, c:uint, color:uint, alpha:Number = 1):void {
        target.graphics.beginFill(color, alpha);
        target.graphics.drawRoundRect(-w*0.5, -h*0.5, w, h, c*2);
        target.graphics.endFill();
    }

}