Mini platform engine
Use arrow keys to move, up arrow to jump
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ch4i
*/
package {
import flash.events.KeyboardEvent;
import flash.geom.Rectangle;
import flash.events.Event;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
[SWF(width = "465", height = "465", frameRate = "15",backgroundColor="0")]
public class FlashTest extends Sprite {
public var myMap:cMap = new cMap();
public var myPlayer:cPlayer = new cPlayer();
// public var map:BitmapData = new BitmapData(64,64,false,0);
public var screen:BitmapData = new BitmapData(64,64,false,0);
public var pic:Bitmap;
public function FlashTest() {
// write as3 code here..
pic = new Bitmap(screen);
pic.scaleX = 8;
pic.scaleY = 8;
addChild(pic);
// map.noise(213);
// map.fillRect(new Rectangle(12,12, 32,32),0);
addEventListener(Event.ENTER_FRAME, onEnter);
stage.addEventListener(KeyboardEvent.KEY_DOWN, kdown);
stage.addEventListener(KeyboardEvent.KEY_UP, kup);
}//ctor
public function kup(e:KeyboardEvent):void
{
cKeyMan.setKey(e.keyCode, false);
}//kup
public function kdown(e:KeyboardEvent):void
{
cKeyMan.setKey(e.keyCode, true);
}//kdn
public function onEnter(e:Event):void
{
update();
render();
}//onenter
public function update():void
{
myPlayer.update();
}//update
public function render():void
{
screen.lock();
screen.draw(myMap.mask);
// screen.draw(map);
screen.setPixel(myPlayer.cx, myPlayer.cy, 0xFFFF0000);
screen.unlock();
}//render
}//classend
}
import flash.display.BitmapData;//package
import flash.geom.Rectangle;
internal class cMap
{
public var mask:BitmapData = new BitmapData(64,64,false,0);
public var mwidth:int = 64;
public var mheight:int = 64;
public static var gmap:cMap; //singleton
public function cMap()
{
mask.noise(214,128,255,7,true);
mask.fillRect(new Rectangle(12,12, 32,32),0);
mask.fillRect(new Rectangle(15,13, 32,32),0);
mask.fillRect(new Rectangle(16,14, 32,32),0);
mask.fillRect(new Rectangle(17,15, 32,32),0);
mask.fillRect(new Rectangle(42,42,8,4),0xFFffFFaa);
mask.fillRect(new Rectangle(22,42,8,4),0xFFffFFaa);
gmap = this;
mwidth = mask.width;
mheight = mask.height;
}//ctor
public function isObstacle(x:int, y:int):Boolean
{
if (x < 0) { return true; }
if (y < 0) { return true; }
if (x >= mwidth) { return true; }
if (y >= mheight) { return true; }
return mask.getPixel(x,y) != 0;
}//isobst
}//cmap
internal class cPlayer
{
public var cx:int = 32;
public var cy:int = 32;
public var jump:int = 0;
public function cPlayer()
{}//ctor
public function update():void
{
var ox:int;
var oy:int;
if (cMap.gmap.isObstacle(cx,cy+1) == false && jump <= 0) {cy+=1;}
ox = cx;
oy = cy;
if (jump > 0) {
if (cMap.gmap.isObstacle(cx,cy-1)) { jump = 0; }
else {jump -= 1; cy -= 1;}
}//endif
if (cKeyMan.isKeyDown(37) )
{ if (cMap.gmap.isObstacle(cx-1,cy)==false) cx -= 1;
else
{
if (cMap.gmap.isObstacle(cx,cy+1)
&& cMap.gmap.isObstacle(cx,cy-1)==false
&& cMap.gmap.isObstacle(cx-1,cy-1)==false)
{
cx -= 1; cy -= 1;
}//endif3
}//endif2
}//endif
if (cKeyMan.isKeyDown(39) )
{
if (cMap.gmap.isObstacle(cx+1,cy)==false)cx += 1;
else
{
if (cMap.gmap.isObstacle(cx,cy+1)
&& cMap.gmap.isObstacle(cx,cy-1)==false
&& cMap.gmap.isObstacle(cx+1,cy-1)==false)
{
cx += 1; cy -= 1;
}//endif3
}//endif2
}//endif
if (cKeyMan.isKeyDown(38) && jump <= 0 )
{
if (cMap.gmap.isObstacle(cx,cy+1))
{
jump = 6;
}
}//endif
//if (cKeyMan.isKeyDown(38) ) { cy -= 1; }
// if (cKeyMan.isKeyDown(40) ) { cy += 1; }
//slope
if (jump <= 0)
{
/* if (cMap.gmap.isObstacle(cx,cy+1))
{
if (cMap.gmap.isObstacle(cx,cy-1)==false ) { cy-=1;}
} */
if (cMap.gmap.isObstacle(cx,cy+2) && cMap.gmap.isObstacle(cx,cy+1) ==false && cMap.gmap.isObstacle(cx,cy) ==false) {cy+=1;}
}//endif
//got stuck in wall
// if (cMap.gmap.isObstacle(cx,cy))
// {cx = ox; cy = oy;}
}//update
}//cplayer
internal class cKeyMan
{
public function cKeyMan() {}//ctor (unused)
// 37 39 38 40
//keycoderef
//http://www.dakmm.com/?p=272
public static var vecKey:Vector.<Boolean> = new Vector.<Boolean>(512,true);
public static function setKey(k:int, b:Boolean):void
{
if (k < 0) return;
if (k >= 512) return;
vecKey[k] = b;
}//setkey
public static function isKeyDown(k:int):Boolean
{
if (k < 0) return false;
if (k >= 512) return false;
return vecKey[k];
}//iskey
}//keyman