flash on 2013-3-2

by ohisama
♥0 | Line 107 | Modified 2013-03-02 19:27:23 | MIT License | (replaced)
play

ActionScript3 source code

/**
 * Copyright ohisama ( http://wonderfl.net/user/ohisama )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/aWSE
 */

package
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.*;
    import flash.utils.*;
    import com.codeazur.as3swf.*;
    import com.codeazur.as3swf.data.*;
    import com.codeazur.as3swf.tags.*;
    import com.bit101.components.*;
    public class FlashTest extends Sprite
    {
        private static const CODE : String =
            "use namespace 'flash.display';\n" +
            "use namespace 'flash.events';\n" +
            "class Movie extends Sprite\n" +
            "{\n" +
            "    public function Movie()\n" +
            "    {\n" +
            "        addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);\n" +
            "    }\n" +
            "    private function onEnterFrameHandler(e : Event) : void\n" +
            "    {\n" +
            "        var mx : int = mouseX;\n" +
            "        var my : int = mouseY;\n" +
            "        graphics.beginFill(0xffff00, 1);\n" +
            "        graphics.drawCircle(24, 24, 12);\n" +
            "        graphics.drawCircle(54, 24, 12);\n" +
            "        graphics.beginFill(0x000000, 1);\n" +
            "        var a : Number;\n" +
            "        var b : Number;\n" +
            "        var x : Number;\n" +
            "        var y : Number;\n" +
            "        a = Math.atan((my - 24) / (mx - 24));\n" +
            "        if (a < 0) b = -6;\n" +
            "        else b = 6;\n" +
            "        x = Math.cos(a) * b + 24;\n" +
            "        y = Math.sin(a) * b + 24;\n" +
            "        graphics.drawCircle(x, y, 6);\n" +
            "        a = Math.atan((my - 24) / (mx - 54));\n" +
            "        if (a < 0) b = -6;\n" +
            "        else b = 6;\n" +
            "        x = Math.cos(a) * b + 54;\n" +
            "        y = Math.sin(a) * b + 24;\n" +
            "        graphics.drawCircle(x, y, 6);\n" +
            "    }\n" +
            "}";
        private var compileStringToBytes : Function;
        private var code : Text;
        private var swf : SWF;
        private var movie : Loader;
        private var data : SWFData;
        public function FlashTest()
        {
            code = new Text(this, 5, 5, CODE);
            code.width = 250;
            code.height = 450;
            addChild(code);
            new PushButton(this, 260, 340, 'make swf', compile);
            movie = new Loader();
            movie.x = 260;
            movie.y = 10;
            addChild(movie);
            var ul : URLLoader = new URLLoader;
            ul.dataFormat = URLLoaderDataFormat.BINARY;
            ul.addEventListener(Event.COMPLETE, loadESC);
            ul.load(new URLRequest("http://assets.wonderfl.net/images/related_images/3/3d/3d72/3d721c692ee6ca816bac3cb996e82a2329c725a2"));
        }
        private function loadESC(e : Event) : void 
        {
            var ul : URLLoader = e.target as URLLoader;
            ul.removeEventListener(Event.COMPLETE, loadESC);
            var data : ByteArray = new ByteArray;
            data.writeBytes(ul.data, 42, ul.data.length - 42);
            var l : Loader = new Loader();
            l.contentLoaderInfo.addEventListener(Event.COMPLETE, completeESC);
            l.loadBytes(data, new LoaderContext(false, ApplicationDomain.currentDomain));
        }
        private function completeESC(e : Event) : void
        {
            compileStringToBytes = ApplicationDomain.currentDomain.getDefinition('ESC.compileStringToBytes') as Function;
        }
        private function compile(e : MouseEvent) : void
        {
            swf = new SWF();
            swf.version = 9;
            swf.frameSize.xmax = 1000;
            swf.frameSize.ymax = 1000;
            swf.frameRate = 30;
            swf.compressed = false;
            swf.tags.push(new TagFileAttributes());
            swf.tags.push(null);
            var tsc : TagSymbolClass = new TagSymbolClass();
            tsc.symbols.push(SWFSymbol.create(0, 'Movie'));
            swf.tags.push(tsc);
            swf.tags.push(new TagShowFrame());
            swf.tags.push(new TagEnd());
            var abc : ByteArray = compileStringToBytes(code.text, 'Movie.as');
            swf.tags[1] = TagDoABC.create(abc, '', false);
            data = new SWFData();
            swf.publish(data);
            movie.unloadAndStop();
            movie.loadBytes(data);
        }
    }
}