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

ひよこちゃんカウンタ

//////////////////////////////////////////////////////////////////////////////
ひよこちゃんカウンタ
[AS3.0] PHPと連携 (1)
http://www.project-nya.jp/modules/weblog/details.php?blog_id=1037
//////////////////////////////////////////////////////////////////////////////
/**
 * Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/krxq
 */

////////////////////////////////////////////////////////////////////////////////
// ひよこちゃんカウンタ
//
// [AS3.0] PHPと連携 (1)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1037
////////////////////////////////////////////////////////////////////////////////

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.net.URLVariables;

    [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 Piyo:Class;
        private var piyo:MovieClip;
        private var plate:Plate;
        private var counter:TextLoader;
        private var filePath:String = "http://www.project-nya.jp/images/flash/php/count.txt";
        private var connector:PHPConnector;
        private var phpPath:String = "http://www.project-nya.jp/images/flash/php/count.php";

        public function Main() {
            //Wonderfl.disable_capture();
            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));
        }
        private function complete(evt:Event):void {
            loader.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 = 180;
            piyo.y = 380;
            piyo.scale = 3;
            piyo.mouseChildren = false;
            piyo.buttonMode = true;
            var mini:MovieClip = new Piyo();
            addChild(mini);
            mini.x = 360;
            mini.y = 440;
            mini.scale = 1.5;
            plate = new Plate();
            addChild(plate);
            plate.x = 360;
            plate.y = 350;
            plate.init({width: 160, height: 40, size: 24});
            counter = new TextLoader();
            counter.addEventListener(Event.COMPLETE, loaded, false, 0, true);
            counter.load(filePath);
            connector = new PHPConnector();
            connector.addEventListener(Event.COMPLETE, update, false, 0, true);
        }
        private function loaded(evt:Event):void {
            counter.removeEventListener(Event.COMPLETE, loaded);
            var data:String = String(evt.target.data);
            plate.show(data);
            piyo.addEventListener(MouseEvent.CLICK, click, false, 0, true);
        }
        private function click(evt:MouseEvent):void {
            connector.connect(phpPath);
        }
        private function update(evt:Event):void {
            var variables:URLVariables = URLVariables(evt.target.data);
            var count:String = variables.count;
            plate.show(count);
        }
        /////////////////////////////////////////////
        //背景
        /////////////////////////////////////////////
        private function draw():void {
            drawSky();
            drawGround();
            drawSun();
            var label:Label = new Label(120, 40);
            label.x = 232 - 60;
            label.y = 100 - 20;
            label.text = "click piyo !";
            label.textColor = 0xFFFFFF;
            label.alpha = 0.6;
            addChild(label);
        }
        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));
        }

    }

}


import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;

class TextLoader extends EventDispatcher {
    private var loader:URLLoader;
    private var _data:*;
    public static const TEXT:String = URLLoaderDataFormat.TEXT;
    public static const BINARY:String = URLLoaderDataFormat.BINARY;
    public static const COMPLETE:String = Event.COMPLETE;

    public function TextLoader() {
        loader = new URLLoader();
    }

    // メソッド
    public function load(file:String, format:String = TextLoader.TEXT):void {
        loader.dataFormat = format;
        loader.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
        loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
        loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
        loader.addEventListener(Event.COMPLETE, complete, false, 0, true);
        try {
            loader.load(new URLRequest(file));
        } catch (err:Error) {
            trace(err.message);
        }
    }
    private function ioerror(evt:IOErrorEvent):void {
        trace(evt.text);
    }
    private function httpstatus(evt:HTTPStatusEvent):void {
        trace(evt.status);
    }
    private function securityerror(evt:SecurityErrorEvent):void {
        trace(evt.text);
    }
    private function complete(evt:Event):void {
        loader.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
        loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
        loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
        loader.removeEventListener(Event.COMPLETE, complete);
        _data = evt.target.data;
        dispatchEvent(new Event(TextLoader.COMPLETE));
    }
    public function get data():* {
        return _data;
    }

}


import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;

class PHPConnector extends EventDispatcher {
    private var loader:URLLoader;
    private var _data:*;
    public static const TEXT:String = URLLoaderDataFormat.TEXT;
    public static const BINARY:String = URLLoaderDataFormat.BINARY;
    public static const VARIABLES:String = URLLoaderDataFormat.VARIABLES;
    public static const COMPLETE:String = Event.COMPLETE;

    public function PHPConnector() {
        loader = new URLLoader();
    }

    public function connect(file:String, variables:URLVariables = null):void {
        loader.dataFormat = PHPConnector.VARIABLES;
        loader.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
        loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
        loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
        loader.addEventListener(Event.COMPLETE, complete, false, 0, true);
        try {
            var request:URLRequest = new URLRequest(file);
            if (variables) {
                request.method = URLRequestMethod.POST;
                request.data = variables;
            }
            loader.load(request);
        } catch (err:Error) {
            trace(err.message);
        }
    }
    private function ioerror(evt:IOErrorEvent):void {
        trace(evt.text);
    }
    private function httpstatus(evt:HTTPStatusEvent):void {
        trace(evt.status);
    }
    private function securityerror(evt:SecurityErrorEvent):void {
        trace(evt.text);
    }
    private function complete(evt:Event):void {
        loader.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
        loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
        loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
        loader.removeEventListener(Event.COMPLETE, complete);
        _data = evt.target.data;
        dispatchEvent(new Event(PHPConnector.COMPLETE));
    }
    public function get data():* {
        return _data;
    }

}


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.DropShadowFilter;
import flash.events.Event;

class Plate extends Sprite {
    private var base:Sprite;
    private var panel:Shape;
    private var arrow:Shape;
    private var txt:TextField;
    private static var fontType:String = "_ゴシック";
    private var px:uint;
    private var py:uint;
    private var _width:uint = 100;
    private var _height:uint = 60;
    private var type:uint = 1;
    private var fontSize:uint = 18;
    private static var xOffset:uint = 15;
    private static var yOffset:uint = 10;
    private static var bColor:uint = 0xFFFFFF;
    private static var sColor:uint = 0x000000;
    private static var tColor:uint = 0x000000;
    private var shade:DropShadowFilter;
    private var _visible:Boolean = false;

    public function Plate() {
        if (stage) {
            initialize(null);
        } else {
            addEventListener(Event.ADDED_TO_STAGE, initialize, false, 0, true);
        }
    }

    public function init(option:Object):void {
        if (option.width != undefined) _width = option.width;
        if (option.height != undefined) _height = option.height;
        if (option.type != undefined) type = option.type;
        if (option.size) fontSize = option.size;
        draw();
    }
    private function draw():void {
        switch (type) {
        case 1 :
            bColor = 0xFFFFFF;
            tColor = 0x000000;
            break;
        case 2 :
            bColor = 0x000000;
            tColor = 0xFFFFFF;
            break;
        }
        shade = new DropShadowFilter(1, 90, sColor, 0.4, 4, 4, 1.5, 2, false, false);
        base = new Sprite();
        panel = new Shape();
        arrow = new Shape();
        txt = new TextField();
        addChild(base);
        base.addChild(panel);
        base.addChild(arrow);
        base.addChild(txt);
        createBox(panel, -_width*0.5, -_height, _width, _height, 4);
        createArrow(arrow, 6, 8);
        base.y = - yOffset;
        txt.x = -_width*0.5 + 5;
        txt.y = -_height*0.9;
        txt.width = _width - 10;
        txt.height = _height;
        txt.type = TextFieldType.DYNAMIC;
        txt.selectable = false;
        //txt.antiAliasType = AntiAliasType.ADVANCED;
        var tf:TextFormat = new TextFormat();
        tf.font = fontType;
        tf.size = fontSize;
        tf.align = TextFormatAlign.CENTER;
        txt.defaultTextFormat = tf;
        txt.textColor = tColor;
        filters = [shade];
    }
    private function initialize(evt:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE, initialize);
    }
    public function show(t:String):void {
        txt.text = t;
    }
    private function createBox(target:Shape, x:int, y:int, w:uint, h:uint, c:uint):void {
        target.graphics.beginFill(bColor);
        target.graphics.drawRoundRect(x, y, w, h, c*2);
        target.graphics.endFill();
    }
    private function createArrow(target:Shape, w:uint, h:uint):void {
        target.graphics.beginFill(bColor);
        target.graphics.moveTo(0, 0);
        target.graphics.lineTo(-w*0.5, 0);
        target.graphics.lineTo(0, h);
        target.graphics.lineTo(w*0.5, 0);
        target.graphics.lineTo(0, 0);
        target.graphics.endFill();
    }

}


import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;

class Label extends Sprite {
    private var txt:TextField;
    private static var fontType:String = "Arial";
    private var txtWidth:uint;
    private var fontSize:uint;

    public function Label(w:uint, s:uint) {
        txtWidth = w;
        fontSize = s;
        draw();
    }

    private function draw():void {
        txt = new TextField();
        txt.width = txtWidth;
        addChild(txt);
        txt.autoSize = TextFieldAutoSize.CENTER;
        txt.type = TextFieldType.DYNAMIC;
        txt.selectable = false;
        //txt.embedFonts = true;
        //txt.antiAliasType = AntiAliasType.ADVANCED;
        var tf:TextFormat = new TextFormat();
        tf.font = fontType;
        tf.size = fontSize;
        tf.align = TextFormatAlign.CENTER;
        txt.defaultTextFormat = tf;
    }
    public function set text(param:String):void {
        txt.text = param;
    }
    public function set textColor(param:uint):void {
        txt.textColor = param;
    }

}