/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/2672
*/
package {
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
vecKey = new Vector.<Boolean>(0,false);
var i:int; for (i =0; i < 512; i++) { vecKey.push(false); }
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKdown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKup);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public function onKdown(e:KeyboardEvent):void { vecKey[e.keyCode] = true; }
public function onKup(e:KeyboardEvent):void { vecKey[e.keyCode] = false; }
public function isKdown(k:int):Boolean { if (k<0||k>=512){return false;} return vecKey[k]; }
public var vecKey:Vector.<Boolean>;
public var cx:Number = 230;
public var cy:Number = 230;
public var vx:Number = 0;
public var vy:Number = 0;
public var vecBlob:Vector.<xBlob>;
public function upBlob(vec:Vector.<xBlob>):void
{
var i:int; var num:int; var a:xBlob;
num = vec.length;
for (i = 0; i < num; i++)
{
a = vec[i];
graphics.drawCircle(a.cx,a.cy, 8);
}//nexti
}//upblob
public function isWall(ax:Number, ay:Number):Boolean
{
if (ax < 0) {return true;} if (ax>460) {return true;}
if (ay < 0) { return true; } if (ay > 460) { return true; }
return false;
}//iswall
public function isOver(r:Number, ax:Number, ay:Number, aw:Number, ah:Number):Boolean
{
graphics.drawRect(ax,ay,aw,ah);
if (cx + r < ax) { return false; } if (cx - r > ax+aw) { return false; }
if (cy + r < ay) { return false; } if (cy - r > ay+ah) { return false; }
return true;
}//isover
public var state:int = 0;
public function setState(i:int):void
{
state = i;
vecBlob = new Vector.<xBlob>(0,false);
}//setstate
public function onEnter(e:Event):void
{
if (state > 0)
{
if (state == 1) { cy += 32; if (cy>=380) { cy=380;state = 0; return; } }
if (state == 2) { cy -= 32; if (cy <= 80) { cy=80; state = 0; return; } }
if (state == 3) { cx += 32; if (cx >= 380) { cx=380; state = 0; return; } }
if (state == 4) { cx -= 32; if (cx <= 80) { cx=80; state = 0; return; } }
}//endif
graphics.clear();
graphics.lineStyle(2, 0);
graphics.beginFill(0x808080,1); graphics.drawRect(0,0,465,465); graphics.endFill();
graphics.beginFill(0,1); graphics.drawRect(0,0,465,16); graphics.endFill();
graphics.beginFill(0,1); graphics.drawRect(0,465-16,465,16); graphics.endFill();
graphics.beginFill(0,1); graphics.drawRect(0,0,16,465); graphics.endFill();
graphics.beginFill(0,1); graphics.drawRect(465-16,0,16,465); graphics.endFill();
graphics.beginFill(0, 1);
graphics.drawCircle(cx,cy, 16);
graphics.endFill();
if (state > 0) { return; }
var rad:Number; rad = 16;
var ms:Number; ms = 8; vx = 0; vy = 0;
if (isKdown(Keyboard.UP)) { vy-= ms; }
if (isKdown(Keyboard.DOWN)) { vy+= ms; }
if (isKdown(Keyboard.LEFT)) {vx-=ms;}
if (isKdown(Keyboard.RIGHT)) {vx+=ms;}
if (vx > 0 && isWall(cx+rad, cy)) { vx =0; }
if (vx < 0 && isWall(cx-rad, cy)) { vx =0; }
if (vy > 0 && isWall(cx, cy+rad)) { vy =0; }
if (vy < 0 && isWall(cx, cy-rad)) { vy =0; }
cx+=vx; cy+=vy;
if (isOver(rad, 180, 0, 64,32)) { setState(1); }
if (isOver(rad, 180, 465-32, 64,32)) { setState(2); }
if (isOver(rad, 0, 180, 32,64)) { setState(3); }
if (isOver(rad, 465-32, 180, 32,64)) { setState(3); }
}//onenter
}//classend
}
internal class xBlob
{
public var cx:Number = 0;
public var cy:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
}//xblob