/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ycwQ
*/
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() {
// write as3 code here..
var a:xRect;
var i:int;
vecRect = new Vector.<xRect>(0, false);
vecRect.push(new xRect(0,300,400,50));
vecRect.push(new xRect(0,0,30,300));
vecRect.push(new xRect(300,0,30,300));
vecRect.push(new xRect(0,20,400,50));
vecRect.push(new xRect(100,100,40,100));
vecRect.push(new xRect(140,150,40,50));
stage.addEventListener(KeyboardEvent.KEY_UP, kup);
stage.addEventListener(KeyboardEvent.KEY_DOWN, kdown);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var vecKey:Vector.<Boolean> = new Vector.<Boolean>(512,false);
public function kdown(e:KeyboardEvent):void { vecKey[e.keyCode] = true;}
public function kup(e:KeyboardEvent):void { vecKey[e.keyCode] = false; }
public var vecRect:Vector.<xRect>;
public var cx:Number = 200;
public var cy:Number = 200;
public var vx:Number = 0;
public var vy:Number = 0;
public var xrad:Number = 8;
public var yrad:Number = 8;
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0xFF);
vy += 0.2;
if (vecKey[Keyboard.UP]) { vy -= 0.5; }
if (vecKey[Keyboard.DOWN]) { vy += 0.1; }
if (vecKey[Keyboard.LEFT]) { vx -= 0.3; }
if (vecKey[Keyboard.RIGHT]) { vx += 0.3; }
if (vx > 8) { vx = 8;}
if (vx <-8) { vx =-8;}
if (vy > 8) { vy = 8;}
if (vy < -8) {vy = -8;}
vx *= 0.99;
vy *= 0.99;
if (vy >= 0 && isOver(cx-xrad+4,cy+yrad,xrad+xrad-8,4))
{ vy = 0; }
if (isOver(cx-xrad+4,cy+yrad-1,xrad+xrad-8,1)) { cy -= 1; }
if (vy < 0 && isOver(cx-xrad+4,cy-yrad-4,xrad+xrad-8,4))
{ vy = 0; }
if (vx < 0 && isOver(cx-xrad-4+vx,cy-2,4-vx,4))
{ vx =0; }
if (isOver(cx-xrad,cy-4,1,8)) { cx+=1;}
if (vx > 0 && isOver(cx+xrad+vx,cy-2,4+vx,4))
{ vx =0; }
if (isOver(cx+xrad-1,cy-4,1,8)) { cx+=1;}
cx += vx;
cy += vy;
graphics.lineStyle(2, 0);
var i:int;
var num:int;
var a:xRect;
num = vecRect.length;
for (i = 0; i < num; i++)
{
a = vecRect[i];
graphics.drawRect(a.cx, a.cy, a.cw, a.ch);
}//nexti
graphics.drawCircle(cx, cy, 8);
graphics.drawRect(cx-xrad,cy-yrad,xrad+xrad,yrad+yrad);
}//onenter
public function isOver(ax:Number, ay:Number, aw:Number, ah:Number):Boolean
{
var i:int;
var num:int;
var a:xRect;
num = vecRect.length;
//debug
graphics.drawRect(ax,ay,aw,ah);
for (i = 0; i < num; i++)
{
a = vecRect[i];
if (a.cx > ax + aw) { continue; }
if (a.cy > ay + ah) { continue; }
if (ax > a.cx+a.cw) { continue; }
if (ay > a.cy+a.ch) { continue; }
return true;
}//nexti
return false;
}//isover
}//classend
}
internal class xRect
{
public var cx:Number = 0;
public var cy:Number = 0;
public var cw:Number = 0;
public var ch:Number = 0;
public function xRect(x:Number, y:Number, w:Number, h:Number):void
{
cx = x; cy = y; cw = w; ch = h;
}
}//xrect