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

10個のMovieClipをrandomで8個並べたい

どっかの質問のAnswer
あってるかどうかはわからんw
Get Adobe Flash player
by narutohyper 08 Feb 2011
    Embed
/**
 * Copyright narutohyper ( http://wonderfl.net/user/narutohyper )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/ijp3
 */

package
{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
        
    public class Main extends Sprite
    {
            private var mc:Array=[]
            private var container:Sprite = new Sprite();
            public function Main() {
                addChild(container)
                
                //並び替えボタン
                var bt:Sprite = new Sprite()
                var label:TextField=new TextField()
                label.autoSize=TextFieldAutoSize.LEFT
                label.mouseEnabled=false;
                label.defaultTextFormat = new TextFormat(null,12,0x000000);
                label.text = '並び替え'
                bt.addChild(label)
                bt.graphics.beginFill(0xCCCCCC);
                bt.graphics.drawRect(0, 0, bt.width, bt.height);
                bt.x = 300
                bt.y = 300
                addChild(bt)
                bt.addEventListener(MouseEvent.CLICK,onClick);
                
                //10のMovieClip
                const colors:Array = [0xFF0000,0x00FF00,0x0000FF,0xFF00FF,0xFFFF00,0x00FFFF,0xFF9900,0xCC9933,0xCCCCCC,0x3399CC]
                var count:int = 10;
                var i:int;
                for (i = 0; i < count; i++)
                {
                    mc[i] = new MovieClip()
                    mc[i].graphics.beginFill(colors[i]);
                    mc[i].graphics.drawRect(0, 0, 100, 100);
                    label=new TextField()
                    label.autoSize=TextFieldAutoSize.LEFT
                    label.mouseEnabled=false;
                    label.defaultTextFormat = new TextFormat(null,30,0x000000);
                    label.text = String(i)
                    mc[i].addChild(label)
                    
                }
                onClick()
            }
            
            private function onClick(e:MouseEvent=null):void
            {
                //繰り返し並べ替える為、最初にcontainerの中をclear
                var count:int = container.numChildren
                var i:int = 0;
                for (i = 0; i < count; i++)
                {
                    container.removeChildAt(0);
                }
                
                //randomで10個のうち8個並べる
                var tempArray:Array = mc.slice();
                count = 8;
                for (i = 0; i < count; i++)
                {
                    var n:int=Math.floor(Math.random()*(10 - i))
                    var tempMc:Array = tempArray.splice(n, 1)
                    container.addChild(tempMc[0]);
                    tempMc[0].x = i%4*110
                    tempMc[0].y = Math.floor(i/4)*110
                }
                
            }
            

    }
}