forked from: sort test
// forked from littlepad's sort test
package {
import flash.display.*;
import flash.text.*;
import caurina.transitions.Tweener;
public class FlashTest extends Sprite {
[SWF(width="465", height="465", backgroundColor="0xFFFFFF", frameRate="60")]
private var _cards:Array = new Array();
private var _tFld:TextField;
public function FlashTest() {
init();
}
//---------------------------------------------------------
// 初期設定
//---------------------------------------------------------
private function init():void {
//TextField設定&配置
_tFld = new TextField();
var tFmt:TextFormat = new TextFormat();
tFmt.color = 0x000000;
tFmt.size = 12;
tFmt.align = TextFormatAlign.CENTER;
_tFld.defaultTextFormat = tFmt;
_tFld.text = "";
_tFld.width = 465;
_tFld.height = 20;
_tFld.x = 0;
_tFld.y = 350;
_tFld.selectable = false;
addChild(_tFld);
var cardAlphabet:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//10枚配置
for (var i:uint = 0; i < 10; i++) {
var card:Card = new Card(cardAlphabet.charAt(i));
_cards.push(card);
addChild(card);
}
shuffle();
var shuffleBtn:Btn = new Btn("シャッフル", 20, 400);
addChild(shuffleBtn);
var alphabetSortBtn:Btn = new Btn("アルファベットでソート", 160, 400);
addChild(alphabetSortBtn);
var noSortBtn:Btn = new Btn("noでソート", 300, 400);
addChild(noSortBtn);
}
//---------------------------------------------------------
// シャッフルメソッド
//---------------------------------------------------------
public function shuffle():void {
for(var i:uint = 0; i < _cards.length; i++) {
Tweener.addTween( _cards[i], {
x:Math.random() * (465 - _cards[i].width) + _cards[i].width/2,
y:Math.random() * (465 - 200) + _cards[i].height/2,
time:1
});
}
_tFld.text = "";
}
//---------------------------------------------------------
// アルファベットでソートするメソッド
//---------------------------------------------------------
public function sortAlphabet():void {
var xPos:Number;
var yPos:Number;
_cards.sortOn("alphabet");
for(var i:uint = 0; i < _cards.length; i++) {
if (i < _cards.length/2) {
xPos = (_cards[i].width + 5) * i + _cards[i].width/2 + 20;
yPos = 115;
} else {
xPos = (_cards[i].width + 5) * (i - _cards.length/2) + _cards[i].width/2 + 20;
yPos = 200;
}
Tweener.addTween( _cards[i], { x:xPos, y:yPos, time:1 });
}
_tFld.text = "アルファベットでソートしました";
}
//---------------------------------------------------------
// Noでソートするメソッド
//---------------------------------------------------------
public function sortNo():void {
var xPos:Number;
var yPos:Number;
//_cards.sortOn("no", Array.NUMERIC);
//↓ sortOnでも出来るけど、あえて比較関数でソートかけてみる
_cards.sort(compare);
for(var i:uint = 0; i < _cards.length; i++) {
if (i < _cards.length/2) {
xPos = (_cards[i].width + 5) * i + _cards[i].width/2 + 20;
yPos = 115;
} else {
xPos = (_cards[i].width + 5) * (i - _cards.length/2) + _cards[i].width/2 + 20;
yPos = 200;
}
Tweener.addTween( _cards[i], { x:xPos, y:yPos, time:1 });
}
_tFld.text = "noでソートしました";
}
//---------------------------------------------------------
// 比較関数
//---------------------------------------------------------
private function compare(a:Card, b:Card):int {
var nA:Number = a.no;
var nB:Number = b.no;
if (nA > nB) {
return 1;
} else if (nA < nB) {
return -1;
} else {
return 0;
}
}
}
}
import flash.display.*;
import flash.text.*;
import flash.events.*;
//---------------------------------------------------------
//
// Btn Class
//
//---------------------------------------------------------
class Btn extends Sprite {
private var _sx:Number; //ボタンのx座標
private var _sy:Number; //ボタンのy座標
private var _sw:Number = 130; //ボタンの縦サイズ
private var _sh:Number = 30; //ボタンの横サイズ
private var _label:String; //ラベル
public function Btn(label:String, sx:Number = 0, sy:Number = 0) {
_label = label;
_sx = sx;
_sy = sy;
init();
}
private function init():void {
//ボタン背景
var square:Shape = new Shape();
square.graphics.beginFill (0x000000, 1);
square.graphics.drawRect(_sx, _sy, _sw, _sh);
addChild(square);
//テキストフォーマット設定
var tFmt:TextFormat = new TextFormat();
tFmt.color = 0xFFFFFF;
tFmt.size = 12;
tFmt.align = TextFormatAlign.CENTER;
//テキスト設定
var tFld:TextField = new TextField();
tFld.defaultTextFormat = tFmt;
tFld.text = _label;
tFld.width = _sw;
tFld.height = tFld.textHeight+4;
tFld.x = _sx;
tFld.y = _sy + _sh/2 - tFld.textHeight/2 - 2;
tFld.selectable = false;
addChild(tFld);
addEventListener(MouseEvent.MOUSE_DOWN, function():void{
y = 2;
switch(_label) {
case "シャッフル":
FlashTest(parent).shuffle();
break;
case "アルファベットでソート":
FlashTest(parent).sortAlphabet();
break;
case "noでソート":
FlashTest(parent).sortNo();
break;
default:
//
}
});
addEventListener(MouseEvent.MOUSE_UP, function():void{y = 0});
}
}
import flash.display.*;
import flash.text.*;
//---------------------------------------------------------
//
// Card Class
//
//---------------------------------------------------------
class Card extends Sprite {
private var _no:uint;
private var _alphabet:String;
public function Card(alphabet:String) {
_alphabet = alphabet;
_no = int(Math.random()*100);
var rect:Shape=new Shape();
rect.graphics.beginFill(0xFFFFFF);
rect.graphics.lineStyle(1,0xAAAAAA);
rect.graphics.drawRect(0,0,80,80);
rect.graphics.endFill();
rect.x = -rect.width/2;
rect.y = -rect.height/2;
addChild(rect);
var tFld:TextField = new TextField();
var tFmt:TextFormat = new TextFormat();
tFmt.color = 0x000000;
tFmt.size = 12;
tFmt.align = TextFormatAlign.CENTER;
tFld.defaultTextFormat = tFmt;
tFld.multiline = true;
tFld.text = _alphabet + "\n" + "no:" + _no;
tFld.width = rect.width;
tFld.height = tFld.textHeight + 8;
tFld.x = -tFld.width/2;
tFld.y = -tFld.height/2;
tFld.selectable = false;
addChild(tFld);
this.alpha = 0.5;
}
public function get no():uint {
return _no;
}
public function get alphabet():String {
return _alphabet;
}
}