/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/KSnp
*/
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() {
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKdown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKup);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var vecKey:Vector.<Boolean> = new Vector.<Boolean>(512,false);
public function isKeyDown(k:int):Boolean { return vecKey[k]==true; }
public function onKdown(e:KeyboardEvent):void { vecKey[e.keyCode] = true; }
public function onKup(e:KeyboardEvent):void { vecKey[e.keyCode] = false; }
public function isWall(ax:Number,ay:Number):Boolean
{ return (ax<0||ax>465||ay>445); }
public function isPlat(ax:Number, ay:Number):Boolean
{ return isWall(ax,ay) || ((ax < wx || ay < wy || ax > wx + ww || ay > wy + wh)==false); }
public function isLadder(ax:Number, ay:Number):Boolean
{ return (ax < rx || ay < ry || ax > rx + rw || ay > ry + rh)==false; }
public var rx:Number = 120;
public var ry:Number = 120;
public var rw:Number = 32;
public var rh:Number = 256;
public var wx:Number = 50;
public var wy:Number = 300;
public var ww:Number = 256;
public var wh:Number = 16;
public var cx:Number = 230;
public var cy:Number = 230;
public var vx:Number = 0;
public var vy:Number = 0;
public var jumpTime:Number = 1;
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0);
graphics.drawRect(0,445,465,20);
var onLadder:Boolean;
onLadder = isLadder(cx,cy);
if (onLadder == false) { vy+=0.4; }
if (onLadder)
{
vy =0;
if (isKeyDown(Keyboard.UP)) { vy -= 4; }
if (isKeyDown(Keyboard.DOWN)) { vy += 4; }
}//endif
vx=0;
if (isKeyDown(Keyboard.LEFT)) { vx -= 4; }
if (isKeyDown(Keyboard.RIGHT)) { vx += 4;}
if (onLadder == false && isKeyDown(Keyboard.UP) && isPlat(cx,cy+16+2)) { vy = -8; jumpTime=8; }
if (vx>0&&isWall(cx+8,cy)) {vx=0;}
else if (vx<0&&isWall(cx-8,cy)) {vx=0;}
var i:int;
if (onLadder)
{
if (vy>0&&isWall(cx,cy+16+vy)) {vy=0;
for (i=0;i<16;i+=1) { if (isWall(cx,cy+16)==false) {cy+=1;} else{break;} }
}
for (i=0;i<16;i+=1) { if (isWall(cx,cy+16)) {cy-=1;} else{break;} }
}
else
{
if (vy>0&&isPlat(cx,cy+16+vy)) {vy=0;
for (i=0;i<16;i+=1) { if (isPlat(cx,cy+16)==false) {cy+=1;} else{break;} }
}
for (i=0;i<16;i+=1) { if (isPlat(cx,cy+16)) {cy-=1;} else{break;} }
}//endif
cx+=vx;cy+=vy;
if (onLadder && isLadder(cx,cy)==false && vy<0) {vy=-8; jumpTime=8;}
graphics.drawRect(rx,ry,rw,rh);
graphics.drawRect(wx,wy,ww,wh);
for(i=0;i<rh;i+=8)
{ graphics.drawRect(rx,ry+i,rw,2); }
graphics.beginFill(0,0.5);
//graphics.drawRect(cx-8,cy-16,16,32);
graphics.drawRect(cx-8-jumpTime,cy-16+jumpTime,16+jumpTime*2,32-jumpTime*2);
graphics.endFill();
jumpTime += (0-jumpTime)*0.2;
}//onenter
}//classend
}