TinyKeyboard grapefrukt forked:3favorite:0lines:34license : All rights reserved modified : 2009-02-13 20:33:15 Embed Tweet package { import flash.display.Sprite; public class STKI extends Sprite { // this stores all key states // For some reason this seems to be smaller when typed public var k:Object; private var block:Sprite; public function STKI () { k = {}; // shorthand for initializing a object stage.addEventListener("keyDown", hk); stage.addEventListener("keyUp", hk); // this is just to show that it works block = new Sprite(); block.graphics.beginFill(0xff00ff); block.graphics.drawRect(-4, -4, 8, 8); block.x = 250; block.y = 250; addChild(block); addEventListener("enterFrame", handleEnterFrame); } // the trick is to use the dynamic nature of objects, // if the property exists it's overwritten, // if it doesn't it's created // the function actually gets a KeyboardEvent, but // having it untyped makes it smaller // So does using a regular ("keyDown") string instead // of the static one provided by the event. // The shorthand if-statement is just for the hell of it // i *think* a full one would compile down to the exact // same size. // to save another few bytes, rename this function to // to something shorter (YMMV) private function hk(e:*):void { k[e["keyCode"]] = e.type == "keyDown" ? true : false; } private function handleEnterFrame(e:*):void{ // this is how you use it, just access the keyCode // in the object, it acts just as good old Key.isDown // from AS2 if (k[37]) { //left block.x -= 1; } else if (k[39]) { // right block.x += 1; } if (k[38]){ // up block.y -= 1; } else if (k[40]) { // down block.y += 1; } } } } Code Fullscreen Preview Fullscreen addEventListener Object addChild Sprite sort new page view favorite forked pv0 forked from: TinyKeyboard xdaben forked:0 favorite:0lines:34 (diff:2) pv472 TinierKeyboard grapefrukt forked:0 favorite:2lines:31 (diff:38) pv202 forked from: TinyKeyboard magicwind forked:0 favorite:0lines:37 (diff:7)