grid move
/**
* Copyright Scmiz ( http://wonderfl.net/user/Scmiz )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/8dLY
*/
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
public class FlashTest extends Sprite {
private const WIDTH:uint = 21;
private const HEIGHT:uint = 21;
private var _unit:Unit;
public function FlashTest() {
var startX:uint = 10;
var startY:uint = 10;
var index:uint = 0;
var flagArray:Array = new Array();
for (index = 0; index < WIDTH * HEIGHT; ++index) {
if (index == (startX) + (startY * WIDTH)) {
flagArray.push(true);
}
else {
flagArray.push(Math.random() < 0.9);
}
}
var gridArray:Array = new Array();
var update:Function = function(x:uint, y:uint):void {
var canMoveArray:Array = new Array();
for (index = 0; index < WIDTH * HEIGHT; ++index) {
canMoveArray.push(false);
}
var chkCanMove:Function = function(rest:uint, x:uint, y:uint):void {
if (rest == 0) return;
if (!flagArray[x + (y * WIDTH)]) return;
canMoveArray[x + (y * WIDTH)] = true;
if (x > 0) {
chkCanMove(rest - 1, x - 1, y);
}
if (x < WIDTH - 1) {
chkCanMove(rest - 1, x + 1, y);
}
if (y > 0) {
chkCanMove(rest - 1, x, y - 1);
}
if (y < HEIGHT - 1) {
chkCanMove(rest - 1, x, y + 1);
}
}
chkCanMove(5, x, y);
for (index = 0; index < WIDTH * HEIGHT; ++index) {
var g:Grid = gridArray[index];
if (!flagArray[index]) continue;
g.draw(canMoveArray[index] ? 0xffff80 : 0x808080);
g.canMove = canMoveArray[index];
g.mouseEnabled = canMoveArray[index];
}
}
var onClickDelegete:Function = function(x:uint, y:uint):Function {
return function(e:MouseEvent):void {
if (!gridArray[x + (y * WIDTH)].canMove) return;
setPos(x, y);
update(x, y);
}
}
for (index = 0; index < WIDTH * HEIGHT; ++index) {
var x:uint = index % WIDTH;
var y:uint = index / WIDTH;
var flag:Boolean = flagArray[index];
var grid:Grid = new Grid(flag ? 0x808080 : 0x000000);
grid.x = x * Constant.GRID_SIZE + 22.5;
grid.y = y * Constant.GRID_SIZE + 22.5;
this.addChild(grid);
gridArray.push(grid);
if (flag) {
grid.addEventListener(MouseEvent.CLICK, onClickDelegete(x, y));
}
grid.mouseEnabled = flag;
}
_unit = new Unit();
setPos(startX, startY);
this.addChild(_unit);
update(startX, startY);
}
private function setPos(x:uint, y:uint):void {
_unit.x = x * Constant.GRID_SIZE + 22.5;
_unit.y = y * Constant.GRID_SIZE + 22.5;
}
}
}
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.SimpleButton;
class Constant {
static public const GRID_SIZE:uint = 20;
}
class Grid extends SimpleButton {
private var _sprite:Sprite;
public var canMove:Boolean = false;
public function Grid(color:uint) {
_sprite = new Sprite();
draw(color);
super(_sprite, _sprite, _sprite, _sprite);
}
public function draw(color:uint):void {
var g:Graphics = _sprite.graphics;
g.clear();
g.lineStyle(1, 0x000000);
g.beginFill(color);
g.drawRect(0, 0, Constant.GRID_SIZE, Constant.GRID_SIZE);
g.endFill();
}
}
class Unit extends Sprite {
public function Unit() {
this.graphics.lineStyle(3, 0x4040ff);
this.graphics.beginFill(0xffffff);
this.graphics.drawCircle(Constant.GRID_SIZE * 0.5, Constant.GRID_SIZE * 0.5, Constant.GRID_SIZE * 0.3);
this.graphics.endFill();
}
}