flash on 2016-4-3
♥0 |
Line 89 |
Modified 2016-04-03 04:24:18 |
MIT License
archived:2017-03-20 16:24:42
ActionScript3 source code
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/6ptu
*/
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() {
initEmpty(40,30);
var i:int;
for(i=0;i<256;i+=1)
{ setTile(Math.random()*mw, Math.random()*mh,1); }
pa = new xAct(); pa.cx = 230; pa.cy=330;
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 var vecGrid:Vector.<int>;
public var mw:int = 0; public var mh:int = 0;
public var cw:Number = 16; public var ch:Number = 16;
public function initEmpty(aw:int, ah:int):void
{ mw=aw; mh=ah; vecGrid = new Vector.<int>(aw*ah, false); }
public function setTile(ax:int, ay:int, t:int):void
{ if (ax<0||ax>=mw||ay<0||ay>=mh) {return;} vecGrid[ay*mw+ax]=t; }
public function getTile(ax:int, ay:int ):int
{ if (ax<0||ax>=mw||ay<0||ay>=mh) {return 0;} return vecGrid[ay*mw+ax]; }
public function isWall(ax:Number,ay:Number):Boolean
{
if (getTile(ax/cw,ay/ch)>0){return true;}
return (ax<0||ay<0||ax>=465||ay>=465);
}//iswall
public var pa:xAct;
public function platMove(a:xAct):void
{
a.canJump = isWall(a.cx,a.cy+4+2) && isWall(a.cx,a.cy)==false;
a.vx*=0.9;
a.vy+=0.2; if (a.vy>3) {a.vy=3;}
if (isWall(a.cx,a.cy)==false)
{
if (a.vx > 0&& isWall(a.cx+4,a.cy)) {a.vx=0;}
else if (a.vx<0 && isWall(a.cx-4,a.cy)) {a.vx=0;}
}//endif
if (a.vy <0 && isWall(a.cx,a.cy-4)&&isWall(a.cx,a.cy-16-4)) {a.vy=0;}
if (a.vy>0 && isWall(a.cx,a.cy+4)) { a.vy=0; }
if (a.vy==0 && isWall(a.cx,a.cy+3)){a.cy-=1;}
a.cx+=a.vx;
a.cy+= a.vy < -3 ? -3 :a.vy;
}//platmove
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0);
var ax:Number; var ay:Number;
var i:int; var k:int; var yt:int; var t:int;
for (i=0;i<mh;i+=1)
{
yt = i * mw;
for (k=0;k<mw;k+=1)
{
t = vecGrid[k+yt];
if (t<=0) { continue; }
ax=k*cw;ay=i*ch;
graphics.beginFill(0x404040, 1);
graphics.drawRect(ax,ay,cw,ch);
graphics.endFill();
continue;
}//nextk
}//nexti
if (isKeyDown(Keyboard.LEFT)) { pa.vx=-2; }
else if (isKeyDown(Keyboard.RIGHT)) { pa.vx=2; }
else if (pa.canJump) {pa.vx*=0.8;}
if (isKeyDown(Keyboard.UP) && pa.canJump) {pa.vy=-5;}
if (isKeyDown(Keyboard.UP)==false && pa.vy<0 && !pa.canJump) {pa.vy+=0.5;}
platMove(pa);
graphics.drawRect(pa.cx-4,pa.cy-4,8,8);
}//onenter
}//classend
}
internal class xAct
{
public var cx:Number = 0;
public var cy:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var canJump:Boolean = true;
}//xact