Comments on: 4k – Tiny Keyboard Handler https://prototyprally.com/4k-tiny-keyboard-handler/ rapid prototyping of games using flash Tue, 14 Feb 2017 09:15:06 +0000 hourly 1 https://wordpress.org/?v=5.1.1 By: grapefrukt https://prototyprally.com/4k-tiny-keyboard-handler/#comment-1015 Tue, 10 Mar 2009 15:46:08 +0000 https://prototyprally.com/?p=232#comment-1015 that’s fantastic, and here I thought this couldn’t get more ugly. nice work!

]]>
By: Clint https://prototyprally.com/4k-tiny-keyboard-handler/#comment-1014 Tue, 10 Mar 2009 15:42:11 +0000 https://prototyprally.com/?p=232#comment-1014 Hey, great tips! I used this technique in my 4k game entry, and actually improved on it a little. In the keyboard handler, change “true” and “false” to be 1 and 0, then in your update function, use:

block.x += k[39] – k[37]; // Right – Left
block.y += k[40] – k[38]; // Down – Up

You have to initialize your k array to be equal to a string of 40 or 50 0’s, but I found that compresses really well, and the bytes saved are more than worth it.

private var k:Array = [0,0,0,0,0,0,0 …. ,0,0,0];

Cheers!

–clint

]]>
By: Richard Davey https://prototyprally.com/4k-tiny-keyboard-handler/#comment-1013 Tue, 24 Feb 2009 11:59:08 +0000 https://prototyprally.com/?p=232#comment-1013 Nice one 🙂 I do use un-typed vars, but not until the very end because it stops lots of the context sensitive assists from working in FlashDevelop! (same for strings replacing constants)

]]>
By: grapefrukt https://prototyprally.com/4k-tiny-keyboard-handler/#comment-1012 Tue, 24 Feb 2009 10:03:56 +0000 https://prototyprally.com/?p=232#comment-1012 I changed the event handlers into inline functions and that saved me a good 17 bytes. Thanks Richard. Using an array instead of an object actually added 2 bytes for me.
You really should try using untyped arguments and plain strings instead of the constants, that’ll save you something like 11 bytes!

]]>
By: Richard Davey https://prototyprally.com/4k-tiny-keyboard-handler/#comment-1011 Mon, 23 Feb 2009 10:25:59 +0000 https://prototyprally.com/?p=232#comment-1011 If you’d like to save yourself even more bytes you could do the following: 1) Use an array instead of an Object and 2) Wrap the function into the event listener call itself. Functions add quite a lot of extra bytes to the SWF, the less of them you have in your final game, the smaller it will be! Here’s the code I use:
stage.addEventListener(KeyboardEvent.KEY_DOWN, function (e:KeyboardEvent) { keys[e.keyCode] = true; });
stage.addEventListener(KeyboardEvent.KEY_UP, function (e:KeyboardEvent) { keys[e.keyCode] = false; } );

]]>
By: grapefrukt https://prototyprally.com/4k-tiny-keyboard-handler/#comment-1010 Sat, 14 Feb 2009 19:20:17 +0000 https://prototyprally.com/?p=232#comment-1010 i think that’s because variables without var are in the global scope, so they might get handled a bit differently.

]]>
By: s https://prototyprally.com/4k-tiny-keyboard-handler/#comment-1009 Fri, 13 Feb 2009 15:12:59 +0000 https://prototyprally.com/?p=232#comment-1009 Why not just go with as2 then? I think I will.

One thing I noticed (flash 8, as2) is that it’s not just about saving characters.

If you declare varibable with var, the swf gets smaller, than if you don’t. Even though it’s the exact same with less characters without it.

]]>
By: grapefrukt https://prototyprally.com/4k-tiny-keyboard-handler/#comment-1008 Fri, 13 Feb 2009 11:27:23 +0000 https://prototyprally.com/?p=232#comment-1008 Using short variable names made more sense when the swf wasn’t compressed, back in the Flash 6 days. Now it should be replaced with some neat compression token, saving you most of that space. But, as you say, they *do* get embedded into the file, so a shorter name should mean a few bytes less in your file.

Also note that the mxmlc compiler, does *not* remove trace statements when you compile for release. These add a handful of bytes each to your code, so make sure you remove them!

The flash authoring tool has an option to do this (omit traces) but mxmlc lacks this for some reason.

]]>
By: Kian https://prototyprally.com/4k-tiny-keyboard-handler/#comment-1007 Fri, 13 Feb 2009 11:13:36 +0000 https://prototyprally.com/?p=232#comment-1007 Beautiful! 😀 One thing; have you tried using
really short variable and function names? I don’t
know about AS3, but in AS2 variable names stay intact
in the actual .swf-file (which is just ridiculous) and
the length of a name even affected performance
at least, that’s what all the cool people said).

]]>