/**
* Copyright dannnn ( http://wonderfl.net/user/dannnn )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/9FsG
*/
/**
* Copyright dannnn ( http://wonderfl.net/user/dannnn )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/9FsG
*/
// forked from YoupSolo's forked from: flash on 2014-8-21
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.GradientType;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
[SWF(width = "465", hight = "465", frameRate = "30")]
public class FlashTest extends Sprite {
public function FlashTest()
{
// you don't need to create a display object (Sprite) for a background and lines
this.graphics.beginFill(0xC2922F);
this.graphics.drawRect(0,0,465,465);
this.graphics.endFill();
// same here draw directly on the stage
this.graphics.beginFill(0x000000);
this.graphics.lineStyle(1);
for ( var h: int = 0; h <= Board.TILE_NUM; h++)
{
var kiftf:TextField = new TextField();
kiftf.x = Board.MARGIN+16+Board.TILE_SIZE*h; // centering the text
kiftf.y = -3; // centering the text
kiftf.defaultTextFormat = new TextFormat("_sans", 10, 0x0);
kiftf.autoSize = TextFieldAutoSize.LEFT;
if (h==0) {
kiftf.text = "9";
}
if (h==1) {
kiftf.text = "8";
}
if (h==2) {
kiftf.text = "7";
}
if (h==3) {
kiftf.text = "6";
}
if (h==4) {
kiftf.text = "5";
}
if (h==5) {
kiftf.text = "4";
}
if (h==6) {
kiftf.text = "3";
}
if (h==7) {
kiftf.text = "2";
}
if (h==8) {
kiftf.text = "1";
}
addChild( kiftf );
kiftf.visible = true;
}
for ( var g: int = 0; g <= Board.TILE_NUM; g++)
{
var kiftf2:TextField = new TextField();
kiftf2.x = Board.MARGIN+Board.TILE_SIZE*9; // centering the text
kiftf2.y = Board.MARGIN+10+Board.TILE_SIZE*g; // centering the text
kiftf.defaultTextFormat = new TextFormat("_sans", 10, 0x0);
kiftf.autoSize = TextFieldAutoSize.LEFT;
if (g==0) {
kiftf2.text = "1";
}
if (g==1) {
kiftf2.text = "2";
}
if (g==2) {
kiftf2.text = "3";
}
if (g==3) {
kiftf2.text = "4";
}
if (g==4) {
kiftf2.text = "5";
}
if (g==5) {
kiftf2.text = "6";
}
if (g==6) {
kiftf2.text = "7";
}
if (g==7) {
kiftf2.text = "8";
}
if (g==8) {
kiftf2.text = "9";
}
addChild( kiftf2 );
kiftf2.visible = true;
}
for ( var i: int = 0; i <= Board.TILE_NUM; i++)
{
this.graphics.moveTo(Board.MARGIN+Board.TILE_SIZE*i, Board.MARGIN);
this.graphics.lineTo(Board.MARGIN+Board.TILE_SIZE*i, Board.MARGIN);
this.graphics.lineTo(Board.MARGIN+Board.TILE_SIZE*i, 370);
this.graphics.endFill()
}
for ( var j: int = 0; j <= Board.TILE_NUM; j++)
{
this.graphics.moveTo(Board.MARGIN, Board.MARGIN+Board.TILE_SIZE*j);
this.graphics.lineTo(Board.MARGIN, Board.MARGIN+Board.TILE_SIZE*j);
this.graphics.lineTo(370, Board.MARGIN+Board.TILE_SIZE*j);
this.graphics.endFill();
}
// filling the board
fillLine2(0);
addChild( new Koma2(1, 1, true) ); // create a Koma at 1,1
addChild( new Koma2(1, 7, true) );
fillLine2(2);
fillLine(6);
addChild( new Koma(7, 1, true) );
addChild( new Koma(7, 7, true) );
fillLine(8);
// you can create one listener to handle the whole DnD mechanic
this.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
this.addEventListener(MouseEvent.MOUSE_UP, onDrop);
}
// fill the line with pawns
private function fillLine(lineNumber:int):void
{
// an option to show or not the name of the pawn
var showNumber:Boolean = true;
for (var index:int = 0; index < 9; index++)
{
// with addchild add directly the pawn to the display list
addChild( new Koma(lineNumber, index, showNumber) );
}
}
private function fillLine2(lineNumber:int):void
{
// an option to show or not the name of the pawn
var showNumber:Boolean = true;
for (var index:int = 0; index < 9; index++)
{
// with addchild add directly the pawn to the display list
addChild( new Koma2(lineNumber, index, showNumber) );
}
}
// on drag behavior
public function onDrag(e:MouseEvent):void
{
// if we click on a Koma, i put this one on the top of the display list ( the pawn is now over the others )
if (e.target is Koma) {
addChild( e.target as Sprite );
}
if (e.target is Koma2) {
addChild( e.target as Sprite );
}
// dragging
e.target.startDrag();
}
public function onDrop(e:MouseEvent):void
{
stopDrag();
// the koma is centered in the curent Tile
if (e.target is Koma)
{
var targetKoma:Koma = e.target as Koma;
var newX:int = Board.MARGIN + Board.KOMA_OFFSET_X + (int((e.stageX - e.localX) / Board.TILE_SIZE)) * Board.TILE_SIZE;
var newY:int = Board.MARGIN + Board.KOMA_OFFSET_Y + (int((e.stageY - e.localY) / Board.TILE_SIZE)) * Board.TILE_SIZE;
// new coordinates
targetKoma.x = newX;
targetKoma.y = newY;
}
if (e.target is Koma2)
{
var targetKoma2:Koma2 = e.target as Koma2;
newX = Board.MARGIN + Board.KOMA_OFFSET_X + (int((e.stageX - e.localX) / Board.TILE_SIZE)) * Board.TILE_SIZE;
newY = Board.MARGIN + Board.KOMA_OFFSET_Y + (int((e.stageY - e.localY) / Board.TILE_SIZE)) * Board.TILE_SIZE;
// new coordinates
targetKoma2.x = newX;
targetKoma2.y = newY;
}
}
}
}
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
class Koma extends Sprite
{
public function Koma(lineNumer:int, index:int, showName:Boolean = false)
{
mouseChildren = false;
mouseEnabled = true;
graphics.beginFill(0xE2A22F);
graphics.lineStyle(1);
graphics.moveTo(15, 0);
graphics.lineTo(15, 0);
graphics.lineTo(25, 5);
graphics.lineTo(30, 30);
graphics.lineTo(0, 30);
graphics.lineTo(5, 5);
x = Board.MARGIN + Board.KOMA_OFFSET_X + index * Board.TILE_SIZE;
y = Board.MARGIN + Board.KOMA_OFFSET_Y + lineNumer * Board.TILE_SIZE;
//name = lineNumer + "_" + index;
if (lineNumer == 6) {
name = "hu";
}
if (lineNumer == 7 && index == 1) {
name = "ka";
}
if (lineNumer == 7 && index == 7) {
name = "hi";
}
if ((lineNumer == 8 && index == 0) || (lineNumer == 8 && index == 8)) {
name = "ky";
}
if ((lineNumer == 8 && index == 1) || (lineNumer == 8 && index == 7)) {
name = "ke";
}
if ((lineNumer == 8 && index == 2) || (lineNumer == 8 && index == 6)) {
name = "gi";
}
if ((lineNumer == 8 && index == 3) || (lineNumer == 8 && index == 5)) {
name = "ki";
}
if ((lineNumer == 8 && index == 4)) {
name = "ou";
}
var tf:TextField = new TextField();
tf.x = 4; // centering the text
tf.y = 8; // centering the text
tf.defaultTextFormat = new TextFormat("_sans", 10, 0x0);
tf.autoSize = TextFieldAutoSize.LEFT;
tf.text = name;
addChild( tf );
tf.visible = showName;
}
}
class Koma2 extends Sprite
{
public function Koma2(lineNumer:int, index:int, showName:Boolean = false)
{
mouseChildren = false;
mouseEnabled = true;
graphics.beginFill(0xE2A22F);
graphics.lineStyle(1);
graphics.moveTo(15, 30);
graphics.lineTo(15, 30);
graphics.lineTo(5, 25);
graphics.lineTo(0, 0);
graphics.lineTo(30, 0);
graphics.lineTo(25, 25);
x = Board.MARGIN + Board.KOMA_OFFSET_X + index * Board.TILE_SIZE;
y = Board.MARGIN + Board.KOMA_OFFSET_Y + lineNumer * Board.TILE_SIZE;
name = lineNumer + "_" + index;
if (lineNumer == 2) {
name = "hu";
}
if (lineNumer == 1 && index == 7) {
name = "ka";
}
if (lineNumer == 1 && index == 1) {
name = "hi";
}
if ((lineNumer == 0 && index == 0) || (lineNumer == 0 && index == 8)) {
name = "ky";
}
if ((lineNumer == 0 && index == 1) || (lineNumer == 0 && index == 7)) {
name = "ke";
}
if ((lineNumer == 0 && index == 2) || (lineNumer == 0 && index == 6)) {
name = "gi";
}
if ((lineNumer == 0 && index == 3) || (lineNumer == 0 && index == 5)) {
name = "ki";
}
if ((lineNumer == 0 && index == 4)) {
name = "ou";
}
var tf:TextField = new TextField();
tf.rotationX = 180;
tf.rotationY = 180;
tf.x = 20; // centering the text
tf.y = 24; // centering the text
tf.defaultTextFormat = new TextFormat("_sans", 10, 0x0);
tf.autoSize = TextFieldAutoSize.LEFT;
tf.text = name;
addChild( tf );
tf.visible = showName;
}
}
// a config object
class Board
{
public static const MARGIN:int = 10;
public static const TILE_SIZE:int = 40;
public static const TILE_NUM:int = 9;
public static const KOMA_OFFSET_X:int = 5;
public static const KOMA_OFFSET_Y:int = 5;
public function Board() {}
}