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

forked from: Ameba Pigg Sample

とりあえずXML化
Get Adobe Flash player
by gmmsh1 05 May 2011
// forked from ahn's Ameba Pigg Sample
package
{    
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.events.Event;
    [SWF(width="465", height="465", frameRate="30", backgroundColor="#FFFFFF")]
    public class Index extends Sprite
    {   
        private var assigneContainer:AssigneContainer;// 着せ替えコンテナ
        private var itemBtn:ItemBtn;// アイテムボタン
        
        // ▼提供素材URL
        // http://www.cyberagent.co.jp/img/media/kayac/piggitem.zip
        public static const PATH:String = "http://assets.wonderfl.net/images/related_images/";
        private var xml:XML = 
            <data>
                <item>
                    <btn>b/b6/b6e2/b6e20fa45c686e63648a95108fb6a71e04da5b0a</btn>
                    <img>a/a9/a974/a9742046e6c944fb9496143f75b5f1d3cd4c4cea</img>
                    <x>5</x>
                    <y>45</y>
                </item>
                <item>
                    <btn>d/df/df7d/df7d681ee045b0a880a4e29b17dd89b49e8f29cc</btn>
                    <img>f/f4/f4b5/f4b57a28591168f37251205710e77fad2c4db7a6</img>
                    <x>30</x>
                    <y>89</y>
                </item>
            </data>;

        public function Index()
        {
            perseXML();
            
            // 着せ替えコンテナの設定と画像配置
            assigneContainer = new AssigneContainer();
            addChild(assigneContainer);
            assigneContainer.loadBody();
        }
        
        private function perseXML():void
        {
            var i:int = 0;
            for each(var node:XML in xml.item)
            {
                var itemData:Object = {};
                for each(var child:XML in node.children())
                { 
                    itemData[child.name()] = child.toString();
                }
                var btn:ItemBtn = new ItemBtn(itemData);
                addChild(btn);
                btn.x = i*50;
                y = 0;
                i++;
            }
        }
    }
}
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Loader;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.system.LoaderContext;

// 着せ替えコンテナ
class AssigneContainer extends Sprite
{
    public static var instance:AssigneContainer;
    private var _itemBmp:Bitmap;
    public function AssigneContainer()
    {
        instance = this;
    }
    
    public function loadBody():void
    {
        var context:LoaderContext = new LoaderContext(true);
        
        // キャラクターを配置
        var body:Loader = new Loader;
        body.contentLoaderInfo.addEventListener( Event.COMPLETE, onLoaded );
        body.load( new URLRequest(Index.PATH+"/8/8d/8db1/8db120b48c6cd0df2826666051dab32687bb0983"), context );
        addChild(body);
        
        // 下着を配置
        var shorts:Loader = new Loader;
        shorts.load( new URLRequest(Index.PATH+"/8/87/87b1/87b1368ba52920859e04095126cf9194849f229f"), context );
        addChild(shorts);
        shorts.x = 38;
        shorts.y = 90;
    }
    
    private function onLoaded(e:Event):void
    {
        //センタリング
        x = (stage.stageWidth-width)/2;
        y = 140;
    }
    
    // アイテムのイメージを表示、非表示
    public function assigne(bmp:Loader, px:int, py:int):void
    {
        if(!bmp.parent)
        {
            addChild(bmp);
            bmp.x = px;
            bmp.y = py;
        }
        else if(!bmp.visible)
        {
            bmp.visible = true;
        }
        else
        {
            bmp.visible = false;
        }
    }
}
// ボタン
class ItemBtn extends Sprite
{
    private var _itemData:Object;
    private var _item:Loader;
    public function ItemBtn(itemData:Object)
    {
        _itemData = itemData;
        var context:LoaderContext = new LoaderContext(true);
        var _loader:Loader = new Loader();
        _loader.load(new URLRequest(Index.PATH+itemData.btn), context);
        _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
        
        // ボタンのイベント設定
        buttonMode = true;
        addEventListener(MouseEvent.MOUSE_UP, clickAction)
    }
    private function loadComplete(event:Event):void
    {
        addChild(event.currentTarget.content);
    }
    private function clickAction(event:MouseEvent):void
    {
        // イベント発信
        //dispatchEvent(new Event("CLICK"));
        var context:LoaderContext = new LoaderContext(true);
        if(!_item)
        {
            _item = new Loader();
            _item.load(new URLRequest(Index.PATH+_itemData.img), context);
        }
        AssigneContainer.instance.assigne( _item, _itemData.x, _itemData.y );
    }
}