Leaflipをwonderflで使ってみた
ドメイン間のSWFリソースの読み込みと、外部SWF内のクラスを活用する方法の確認。
バイナリで読み込むことができれば、サンドボックス侵害はもう気にしなくていいらしい。
つまずいた所は、crossdomain.xmlでのドメイン指定と、allowDomainを忘れていたところ。
ページめくりが簡単にできる素敵ライブラリ「Leaflip]についてはこちらへ
http://www.eqliquid.com/products/leaflip/index.html
/**
* Copyright codalx ( http://wonderfl.net/user/codalx )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/hQ9lx
*/
/*
異なるドメインにあるSWFを用いて、クラスオブジェクトを扱う方法の確認
Leaflip
Copyright (c) <2010> SionDesign (www.sionnet.jp)
eqLiquid (www.eqliquid.com)
*/
package {
import flash.text.TextFormat;
import flash.system.ApplicationDomain;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Graphics;
import flash.display.Loader;
import flash.text.TextField;
import flash.system.Security;
import flash.system.LoaderContext;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
[SWF(width="465", height="465", backgroundColor="0xFFFFFF", framerate="30")]
public class Test extends Sprite
{
public function Test()
{
if(stage) _init();
else addEventListener(Event.ADDED_TO_STAGE, _init);
}
private const package_leaflip : String = "jp.sionnet.leaflip.";
private const package_tinytweener : String = "jp.sionnet.tinytweener.";
private const SIZE : int = 300;
private const uri : String = "http://ciela-leone.org";
private const swf : String = "/libs/leaflip.bin.swf";
private var _urlLoader : URLLoader;
private var _binLoader : Loader;
private function _init(...p):void
{
//これを忘れてた。あと、読み込むSWFにも指定することを忘れてて結構悩んでた…
//メモ:allowDomainするURIは「http://wonderfl.net」と「swf.wonderfl.net」の2つ
Security.allowDomain(uri);
_binLoader = new Loader();
_urlLoader = new URLLoader();
_urlLoader.dataFormat = "binary";
_setEventHandler(_urlLoader);
_urlLoader.load(new URLRequest(uri + swf));
}
private function _setEventHandler(loader : IEventDispatcher):void
{
loader.addEventListener(Event.COMPLETE, _onComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR, _onError);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _onError);
}
private function _unsetEventHandler(loader : IEventDispatcher):void
{
loader.removeEventListener(Event.COMPLETE, _onComplete);
loader.removeEventListener(IOErrorEvent.IO_ERROR, _onError);
loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, _onError);
}
private function _onError(...p):void
{
//
}
private function _onComplete(e:Event):void
{
switch(e.target)
{
case _urlLoader :
{
_unsetEventHandler(_urlLoader);
_setEventHandler(_binLoader.contentLoaderInfo);
_binLoader.loadBytes(_urlLoader.data, new LoaderContext(false, ApplicationDomain.currentDomain, null));
break;
}
case _binLoader.contentLoaderInfo :
{
_unsetEventHandler(_binLoader.contentLoaderInfo);
_setup();
break;
}
}
}
private function _setup():void
{
var _page1 : * = new (_extract(package_leaflip + "Leaflip"))(SIZE, SIZE, 30);
var _page2 : * = new (_extract(package_leaflip + "Leaflip"))(SIZE, SIZE, 30);
var _shape1 : Shape = new Shape;
var _shape2 : Shape = new Shape;
var _shape3 : Shape = new Shape;
var _shape4 : Shape = new Shape;
_drawRoundRect(_shape1.graphics, 0xCCCCCC);
_drawRoundRect(_shape2.graphics, 0xA0A0A0);
_drawRoundRect(_shape3.graphics, 0xCECECE);
_drawRoundRect(_shape4.graphics, 0x999999);
_page1["addFSP"](_shape1);
_page1["addBSP"](_shape2);
_page2["addFSP"](_shape3);
_page2["addBSP"](_shape4);
_page1["allowFlip"] =
_page2["allowFlip"] = true;
_page1["setGradient"](0x333333, 128);
_page2["setGradient"](0x000000, 64);
with (addChild(_page1 as DisplayObject))
{
x =
y = (465 - SIZE) / 2;
}
with(addChildAt(_page2 as DisplayObject, 0))
{
x =
y = (465 - SIZE) / 2;
}
([_page1, _page2]).forEach(function(...p):void{
p[0].addEventListener("pick", function(...q):void{
p[2].forEach(function(...r):void{
r[0].allowFlip = false;
});
});
p[0].addEventListener("closecomplete", function(...q):void{
p[2].forEach(function(...r):void{
r[0].allowFlip = true;
});
});
p[0].addEventListener("opencomplete", function(...q):void{
addChildAt(q[0].target, 0);
q[0].target.alpha = 1;
q[0].target.reset();
p[2].forEach(function(...r):void{
r[0].allowFlip = true;
});
});
});
}
private function _drawRoundRect(g:Graphics, color:uint):void
{
with (g)
{
beginFill(color);
drawRoundRect(0, 0, SIZE, SIZE, 30, 30);
endFill();
}
}
private function _extract(className:String):Class
{
return _binLoader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
}
}
}