flash on 2011-3-22
/**
* Copyright kingdrake ( http://wonderfl.net/user/kingdrake )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/fNSV
*/
package {
import flash.display.Sprite;
import flash.events.*;
public class FlashTest extends Sprite {
public function FlashTest() {
var bWidth:Number = 400;
var bHeight:Number = 350;
var grid:Number = 20; // Size of the grid and number of lattice points in each direction
var dotsWide:Number = Math.ceil(bWidth/grid) - 1;
var dotsHigh:Number = Math.ceil(bHeight/grid) - 1;
var board:Sprite = new Sprite();
var myPoint:Sprite = new Sprite();
this.addChild(board);
board.addChild(myPoint);
board.graphics.lineStyle(1,0);
board.graphics.beginFill(0xCCCCCC);
board.graphics.drawRect(0,0,bWidth,bHeight);
board.graphics.endFill();
// Add a bunch of circles to represent lattice points
board.graphics.beginFill(0x000000);
for (var i:int=1; i<=dotsHigh; i++) {
for (var j:int=1; j<=dotsWide; j++) {
board.graphics.drawCircle(j*grid,i*grid,0.5);
}
}
board.graphics.endFill();
board.x = 10;
board.y = 10;
myPoint.graphics.lineStyle(2,0xEEEEEE, .4);
myPoint.graphics.beginFill(0xDDAA77);
myPoint.graphics.drawCircle(0,0,8);
myPoint.graphics.endFill();
myPoint.x = goodX(50);
myPoint.y = goodY(50);
myPoint.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
function startMove(evt:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_MOVE, pointMove);
}
function pointMove(e:MouseEvent):void {
myPoint.x = goodX(board.mouseX);
myPoint.y = goodY(board.mouseY);
e.updateAfterEvent();
}
stage.addEventListener(MouseEvent.MOUSE_UP, stopAll);
function stopAll(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, pointMove);
}
function goodX(inX:Number):Number {
if (inX > grid*dotsWide) {
return grid*dotsWide;
}
if (inX < grid) {
return grid;
}
return grid*Math.round(inX/grid);
}
function goodY(inY:Number):Number {
if (inY > grid*dotsHigh) {
return grid*dotsHigh;
}
if (inY < grid) {
return grid;
}
return grid*Math.round(inY/grid);
}
}
}
}