Line Tracing
/**
* Copyright jokehn9 ( http://wonderfl.net/user/jokehn9 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/m8Du
*/
package {
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Shader;
import flash.display.Shape;
import flash.events.Event;
import flash.events.MouseEvent;
public class FlashTest extends Sprite {
public function FlashTest() {
// write as3 code here..
//set Dots of body of nxt
var point1:Array = [0,0];
var point2:Array = [40,0];
var point3:Array = [40,40];
var point4:Array = [0,40];
var dots:Array = [point1,point2,point3,point4];
//set Dots of screen
var sPoint1:Array = [4,4];
var sPoint2:Array = [38,4];
var sPoint3:Array = [38,16];
var sPoint4:Array = [4,16];
var sPoints:Array = [sPoint1,sPoint2,sPoint3,sPoint4];
//set Center button of NXT
var cPoint:Array = [20,30];
var nxt:Sprite = new Sprite();
var nxtColor:uint = 0x999999;
var screenColor:uint = 0x8764;
//make robot's body
makeBody(nxt,dots,nxtColor);
makeBody(nxt,sPoints,screenColor);
//nxt's center button
nxt.graphics.beginFill(0xff8800);
nxt.graphics.drawRect(cPoint[0]-6,cPoint[1]-6,12,12);
//nxt's 4 circles;
nxt.graphics.beginFill(0x0);
nxt.graphics.drawRect(-8,12,8,12);
//nxt.graphics.drawRect(-8, 32,8,12);
nxt.graphics.drawRect(40,12,8,12);
nxt.graphics.drawRect(17,40,6,6);
//nxt's right sensor;
nxt.graphics.lineStyle(4,0xcccccc,0.9);
nxt.graphics.moveTo(2,0);
nxt.graphics.lineTo(-4,-12);
nxt.graphics.moveTo(38,0);
nxt.graphics.lineTo(42,-12);
nxt.graphics.beginFill(0x0,0.9);
nxt.graphics.drawRect(-6,-16,4,4);
nxt.graphics.drawRect(40,-16,4,4);
//nxt's first position;
var fPositionx:Number = 100;
var fPositiony:Number = 330;
nxt.x = fPositionx;
nxt.y = fPositiony;
// show your robot
nxt.rotation += 90;
var mapData:BitmapData = new BitmapData(1257,433);
// mapData.draw(img);
var mapShape:Shape = new Shape();
mapShape.graphics.lineStyle(3,0x0);
mapShape.graphics.moveTo(fPositionx+30,fPositiony);
mapShape.graphics.curveTo(465,232,fPositionx, 20);
mapData.draw(mapShape);
//move nxt;
var vx:Number = 1;
var vy:Number = 0;
var nxtrotation:Number = 0;
function checkRect(bd:BitmapData,nxt:Sprite, fx:int, fy:int,rota:Number):int
{
var i:int = 0;
var k:int = 0;
var u:uint = 0;
var transformx:Number = 0;
var transformy:Number = 0;
// 4,-10
for (i=0; i<4; i++)
{
for (k=0; k<4; k++)
{
transformx = Math.cos(rota)*(i+fx) - Math.sin(rota)*(k+fy);
transformy = Math.cos(rota)*(k+fy) +Math.sin(rota)*(i+fx);
transformx += nxt.x;
transformy += nxt.y;
u = bd.getPixel(transformx,transformy);
if (u == 0x0)
{
if(transformx < 0 ) {
return 0;
}
else if(transformx > 465 ) {
return 0;
}
else if( transformy < 0) {
return 0;
}
else if(transformy > 465) {
return 0;
}
return 1;
}
}
}
return 0;
}
var startV:int = 0;
var linex:Number = 465;
var liney:Number = 232;
var som20:Number = 20;
stage.addEventListener(MouseEvent.CLICK,clicks);
function clicks(e:MouseEvent):void
{
if(startV == 0) {
startV = 1;
}
if(startV == 2) {
nxt.x = fPositionx;
nxt.y = fPositiony;
nxt.rotation = 90;
startV = 1;
mapShape.graphics.lineStyle(10,0xffffff);
mapShape.graphics.moveTo(fPositionx+30,fPositiony);
mapShape.graphics.curveTo(linex ,liney ,fPositionx, 20);
if(mouseX > 240 && mouseY < 350) {
linex = mouseX;
liney = mouseY;
}
else {
linex = 465;
liney = 232;
}
mapShape.graphics.lineStyle(3,0x0);
mapShape.graphics.moveTo(fPositionx+30,fPositiony);
mapShape.graphics.curveTo(linex ,liney ,fPositionx, 20);
mapData.draw(mapShape);
}
}
addEventListener("enterFrame",moveNXT);
var cL:int = 0;
var cR:int = 0;
var count:int = 0;
function moveNXT(e:Event):void
{
if (startV == 1)
{
if (nxt.x <150)
{
if (nxt.y < 150)
{
nxt.x += 4 * vx;
nxt.y += 4 * vy;
startV = 2;
}
}
nxtrotation = Math.PI * 2 * nxt.rotation / 360;
vx = 9 * Math.sin(nxtrotation);
vy = -9 * Math.cos(nxtrotation);
cL = checkRect(mapData,nxt,-6,-16,nxtrotation);
cR = checkRect(mapData,nxt,42,-16,nxtrotation);
if (cL == 1)
{
nxt.rotation -= 10;
count++;
if (count > 10)
{
nxt.x -= vx;
nxt.y -= vy;
}
}
else if (cR== 1)
{
nxt.rotation += 10;
count++;
if (count > 10)
{
nxt.x -= vx;
nxt.y -= vy;
}
}
else
{
count = 0;
nxt.x += vx;
nxt.y += vy;
}
}
}
//Show
var map:Bitmap = new Bitmap(mapData);
addChild(map);
addChild(nxt);
function makeBody(robot:Sprite , arr:Array, color:uint):void
{
robot.graphics.beginFill(color);
robot.graphics.moveTo(arr[0][0],arr[0][1]);
for (var i:int = 1; i < arr.length; i++)
{
robot.graphics.lineTo(arr[i][0],arr[i][1]);
}
}
}
}
}