flash on 2012-7-12
Just another failed experiment
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/aVaQ
*/
package {
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public var map:Array =
[
1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,1,0,0,1,
1,0,0,0,0,0,1,0,0,1,
1,0,0,0,0,0,1,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,1,1,0,0,1,1,0,0,1,
1,1,1,0,0,1,1,0,1,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,1,0,0,1,
1,1,1,1,1,1,1,1,1,1
];
public var mwidth:int = 10;
public var mheight:int = 10;
public var vecMap:Vector.<int> = new Vector.<int>;
public var cw:Number = 32;
public var ch:Number = 32;
public var px:Number = 64;
public var py:Number = 64;
public var pr:Number = 16;
public var vx:Number = 0;
public var vy:Number = 0;
public var maxpx:Number = 0;
public var maxpy:Number = 0;
public var minpx:Number = 0;
public var minpy:Number = 0;
public function FlashTest() {
// write as3 code here..
var total:int;
total = mwidth * mheight;
vecMap = new Vector.<int>(total,false);
var i:int;
var num:int;
num = total;
for (i = 0; i < num; i++)
{ vecMap[i] = map[i]; }
stage.addEventListener(Event.ENTER_FRAME, onEnter);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
}//ctor
public function isWall(wx:Number, wy:Number):Boolean
{
var tx:int;
var ty:int;
tx = Math.floor(wx / cw);
ty = Math.floor(wy / ch);
if (tx < 0) { return true; }
if (ty < 0) { return true; }
if (tx >= mwidth) { return true; }
if (ty >= mheight) { return true; }
return (vecMap[(ty*mwidth)+tx] > 0);
}//iswall
public var kUp:Boolean = false;
public var kDown:Boolean = false;
public var kRight:Boolean = false;
public var kLeft:Boolean = false;
public function keyDown(k:KeyboardEvent):void
{
switch (k.keyCode)
{
case Keyboard.LEFT: kLeft = true; break;
case Keyboard.RIGHT: kRight = true; break;
case Keyboard.DOWN: kDown = true; break;
case Keyboard.UP: kUp = true; break;
}//swend
}//kdown
public function keyUp(k:KeyboardEvent):void
{
switch (k.keyCode)
{
case Keyboard.LEFT: kLeft = false; break;
case Keyboard.RIGHT: kRight = false; break;
case Keyboard.DOWN: kDown = false; break;
case Keyboard.UP: kUp = false; break;
}//swend
}//kup
public function Draw():void
{
var i:int;
graphics.clear();
graphics.lineStyle(1,0);
var k:int;
var yt:int;
var tile:int;
for (i=0; i < mheight;i++)
{
yt = i * mwidth;
for (k = 0; k < mwidth; k++)
{
tile = vecMap[yt+k];
if (tile <= 0) { continue; }
graphics.drawRect(k*cw,i*ch,cw,ch);
}//nextk
}//nexti
graphics.drawCircle(px,py,pr);
graphics.lineStyle(1, 0xFF0000);
var m:int;
var mk:Number;
var maxk:Number;
mk = px;
for (m = 0; m < 16; m+= 1)
{
graphics.drawCircle(mk,py,4);
if (isWall(mk, py))
{
maxk = Math.floor(mk / cw) * cw;
graphics.drawCircle(maxk,py, 9);
break;
}
mk += 16;
}//nextm
//if (px > (maxk-pr)) {px = mk-pr; vx = 0;}
maxpx = maxk - pr;
mk = px;
for (m = 0; m < 16; m+= 1)
{
graphics.drawCircle(mk,py,4);
if (isWall(mk, py))
{
maxk = cw+Math.floor(mk / cw) * cw;
graphics.drawCircle(maxk,py, 9);
break;
}
mk -= 16;
}//nextm2
minpx = maxk + pr;
mk = py;
for (m =0; m < 16; m+=1)
{
graphics.drawCircle(px,mk,4);
if (isWall(px,mk))
{
maxk = Math.floor(mk / ch) * ch;
graphics.drawCircle(px,maxk, 9);
break;
}//endif
mk += 16;
}//nextm3
maxpy = maxk -pr;
mk = py;
for (m =0; m < 16; m+=1)
{
graphics.drawCircle(px,mk,4);
if (isWall(px,mk))
{
maxk = ch+Math.floor(mk / ch) * ch;
graphics.drawCircle(px,maxk, 9);
break;
}//endif
mk -= 16;
}//nextm4
minpy = maxk + pr;
}//draw
public function onEnter(e:Event):void
{
if (kRight) { vx += 0.4; }
if (kLeft) { vx -= 0.4; }
if (kUp ) { vy -= 0.4; }
if (kDown) { vy += 0.4; }
vx *= 0.91;
vy *= 0.91;
if (vx > 0 && px >= maxpx) { vx = 0; px = maxpx; }
if (vx < 0 && px <= minpx) { vx = 0; px = minpx; }
if (vy > 0 && py >= maxpy) { vy = 0; py = maxpy; }
if (vy < 0 && py <= minpy) { vy = 0; py = minpy; }
px += vx;
py += vy;
Draw();
}//onenter
}//classend
}