ひとつの配列でやりくり
forked from 10個のMovieClipをrandomで8個並べたい (diff: 84)
参考: http://www.fumiononaka.com/TechNotes/Flash/FN0212002.html http://www.fumiononaka.com/TechNotes/Flash/FN0212003.html
ActionScript3 source code
/**
* Copyright matacat ( http://wonderfl.net/user/matacat )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ciQ9
*/
// forked from narutohyper's 10個のMovieClipをrandomで8個並べたい
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Main extends Sprite
{
private var array:Array = [];
private var container:Sprite = new Sprite();
public function Main()
{
addChild(container);
//並び替えボタン
var bt:TF = new TF("並び替え", 12, 0xcccccc, 0, 0);
bt.autoSize = "left";
bt.x = 300;
bt.y = 300;
bt.addEventListener(MouseEvent.CLICK, onClick);
addChild(bt);
//10のMovieClip
var colors:Array = [0xcccccc, 0xff0000, 0xffaa00, 0xaaff00, 0x00ff00, 0x00ffaa, 0x00aaff, 0x0000ff, 0xaa00ff, 0xff00aa];
var count:int = colors.length;
for (var i:int = 0; i < count; i++) {
array[i] = new TF(String(i), 30, colors[i], 100, 100);
}
onClick();
}
private function onClick(e:MouseEvent = null):void
{
var i:int,
j:int,
t:TF,
count:int;
//繰り返し並べ替える為、最初にcontainerの中をclear
for (i = 0, count = container.numChildren; i < count; i++) {
container.removeChildAt(0);
}
//randomで10個のうち8個並べる
for (i = 0, count = 8; i < count; i++) {
j = i + 1;
j = j + Math.floor( Math.random() * (array.length - j) );
t = array[j];
t.x = i % 4 * 110;
t.y = Math.floor(i / 4) * 110;
container.addChild(t);
array[j] = array[i];
array[i] = t;
}
}
}
}
import flash.text.TextField;
import flash.text.TextFormat;
class TF extends TextField
{
public function TF(labelText:String, fontSize:Number, bgColor:uint, width:Number, height:Number)
{
background = true;
backgroundColor = bgColor;
defaultTextFormat = new TextFormat(null, fontSize, 0x000000);
selectable = false;
text = labelText;
this.width = width;
this.height = height;
}
}
