// forked from a8085's Bubble Sort // write as3 code here.. package { import flash.display.*; import flash.events.*; import flash.media.*; import flash.net.*; import flash.utils.*; public class BubbleSort extends Sprite { //並び替えを行うアイテムを入れておく配列 private var items:Array = new Array(); //並び替えを行うタイマー private var sortTimer:Timer = new Timer(1); //配色 private var colorSet:Array = [0xff7f7f, 0xff7fbf, 0xff7fff, 0xbf7fff, 0x7f7fff, 0x7fbfff, 0x7fffff, 0x7fffbf, 0x7fff7f, 0xbff7f, 0xffff7f, 0xffbf7f] private var colorIndex:uint = 0; //並び替え用の変数 private var position:int; private var lastPosition:int; public function BubbleSort():void { super(); var WIDTH:uint = 456; var HEIGHT:uint = 456; //var HEIGHT:uint = 64; var itemX:uint = 2; var itemY:uint = 2; var target:uint = 0; //アイテムの配置 while(itemY < HEIGHT){ while(itemX < WIDTH){ var item:NumberItem = new NumberItem(Math.round(Math.random() * 100)); item.x = itemX; item.y = itemY; this.addChild(item); items.push(item); itemX += 36; } itemX = 2; itemY = item.y + 36; } //並び替え変数の初期化 position = 0; lastPosition = items.length -1; //並び替え開始 sortTimer.addEventListener(TimerEvent.TIMER, bubbleSort); sortTimer.start(); } //バブルソート private function bubbleSort(e:TimerEvent):void { if(position < lastPosition){ position++; } if( (items[position - 1] as NumberItem).value > (items[position] as NumberItem).value ){ this.swap(position - 1, position); } if(position == lastPosition){ lastPosition--; position = 0; } if(lastPosition == 0){ sortTimer.stop(); } } //スワップ private function swap(index1:Number, index2:Number):void{ var tmp:Number = (items[index1] as NumberItem).value; (items[index1] as NumberItem).value = (items[index2] as NumberItem).value; (items[index2] as NumberItem).value = tmp; var color:Number = nextColor(); (items[index1] as NumberItem).illuminate(color); (items[index2] as NumberItem).illuminate(color); } //配色取得 private function nextColor():Number { if(colorSet.length - 1 <= colorIndex){ colorIndex = 0; }else{ colorIndex++; } return colorSet[colorIndex]; } } } import flash.display.*; import flash.text.*; import flash.filters.*; import flash.utils.*; import flash.events.*; //アイテムクラス class NumberItem extends Sprite { private var itemText:TextField = new TextField(); private var textFormat:TextFormat = new TextFormat(); private var innerCircle:Sprite = new Sprite(); private var timer:Timer = new Timer(10); public function NumberItem(number:Number):void { var dsFilter:DropShadowFilter = new DropShadowFilter(0.5, 45, 0x666666); this.filters = [dsFilter]; this.graphics.beginFill(0xFFFFFF); this.graphics.drawCircle(16, 16, 16); this.graphics.endFill(); innerCircle.graphics.beginFill(0xFFFFFF); innerCircle.graphics.drawCircle(16, 16, 16); innerCircle.graphics.endFill(); this.addChild(innerCircle); textFormat.size = 14; textFormat.align = TextFormatAlign.CENTER; textFormat.bold = true; itemText = new TextField(); itemText.width = 32; itemText.y = 5; this.value = number; this.updateTextFormat(); this.addChild(itemText); timer.addEventListener(TimerEvent.TIMER, timerHandler); } //文字のスタイルを設定 private function updateTextFormat():void { itemText.setTextFormat(textFormat); } //ハイライト処理。ハイライト後、フェードアウト public function illuminate(color:Number):void { timer.reset(); innerCircle.alpha = 1; innerCircle.graphics.clear(); innerCircle.graphics.beginFill(color); innerCircle.graphics.drawCircle(16, 16, 16); innerCircle.graphics.endFill(); this.updateTextFormat(); timer.start(); } //色のフェードアウト処理 private function timerHandler(e:TimerEvent):void { //trace(innerCircle.alpha); innerCircle.alpha -= 0.1; if(innerCircle.alpha < 0) { trace("stop"); innerCircle.graphics.clear() innerCircle.alpha = 1; innerCircle.graphics.beginFill(0xFFFFFF); innerCircle.graphics.drawCircle(16, 16, 16); innerCircle.graphics.endFill(); timer.reset(); } } public function get value():Number { return Number(itemText.text); } public function set value(number:Number):void { this.itemText.text = number.toString(); } } forked from: Bubble Sort