Category Archives: associative arrays

Dictionary with ES6 Symbol

Creating a dictionary type object with ES6 Symbols is easy. Yes we have Maps and WeakMaps but this is still interesting for a variety of reasons… Being able to use objects as keys in another object (dictionary) has many great uses…. So how do you use Symbols like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
let a = { id: Symbol('key') },
    b = { id: Symbol('key') };
 
let dictionary = {
  [a.id]: 'value by obj a',
  [b.id]: 'value by obj b'
};
 
console.log(dictionary[a.id]);
console.log(dictionary[b.id]);
 
// outputs:
// 'value by obj a'
// 'value by obj b'

By using either object a or object b’s `id` symbol, our dictionary points to another value. This old AS3 snippet is similar:

http://actionsnippet.com/?p=426

Also posted in Dictionary, arrays, es6, html5, javascript | Tagged , , , | Leave a comment

Vector.sortOn()

Actionscript:
  1. var a:Vector.<Sprite> = new Vector.<Sprite>();
  2.  
  3. trace("unsorted");
  4. for (var i:int = 0; i<10; i++){
  5.     var s:Sprite = new Sprite();
  6.     s.x = int(Math.random()*100);
  7.     a.push(s);
  8.     trace(s.x);
  9. }
  10.  
  11. quickSortOn(a, "x", 0, a.length-1);
  12.  
  13. trace("sorted");
  14. for (i= 0; i<10; i++){
  15.     trace(a[i].x);
  16. }
  17.  
  18. // modified code from kirupa.com
  19. // http://www.kirupa.com/developer/actionscript/quickSort.htm
  20. function quickSortOn(a:Vector.<Sprite>, prop:String, left:int, right:int):void {
  21.     var i:int = 0, j:int = 0, pivot:Sprite, tmp:Sprite;
  22.     i=left;
  23.     j=right;
  24.     pivot = a[Math.round((left+right)*.5)];
  25.     while (i<=j) {
  26.         while (a[i][prop]<pivot[prop]) i++;
  27.         while (a[j][prop]>pivot[prop]) j--;
  28.         if (i<=j) {
  29.             tmp=a[i];
  30.             a[i]=a[j];
  31.             i++;
  32.             a[j]=tmp;
  33.             j--;
  34.         }
  35.     }
  36.     if (left<j)  quickSortOn(a, prop, left, j);
  37.     if (i<right) quickSortOn(a, prop, i, right);
  38. }
  39. /* outputs something like:
  40. unsorted
  41. 26
  42. 33
  43. 20
  44. 63
  45. 7
  46. 68
  47. 75
  48. 39
  49. 67
  50. 53
  51. sorted
  52. 7
  53. 20
  54. 26
  55. 33
  56. 39
  57. 53
  58. 63
  59. 67
  60. 68
  61. 75
  62. */

This demo is my first quick stab at using at a sortOn() function for the Vector class. It sorts a Vector of Sprites by their x property.

Recently there were a few times when I was prototyping ideas and suddenly realized that I needed to change my Vector to an Array because I needed to use sortOn().(If you don't already know, there is no built in sortOn() method for the Vector class). In the past I spent some time with sorting algorithms, bubble, insertion etc... so I knew I could pretty easily write my own sortOn(), but I also realized that a generic implementation wouldn't be easy/possible without loosing the type of the Vector. What I mean is, if you have a Vector of Sprites, you need a sorting method that takes a Vector.< Sprite > type as an argument (as seen above), if you have a Vector of TextFields you need a Vector.< TextField > type as an argument. You could of course use a generic type, but this kind of defeats the purpose of using a vector in the first place...

I will likely post a revised version of this in the near future with a slightly improved implementation of QuickSort. I haven't spent that much time with this, but If I recall correctly this is not the ideal implementation. I ported this code from a nice Kirupa tutorial and modified it to sort based on a property...

Also posted in Object, Vector, arrays, sortOn | Tagged , | 3 Comments

Multiple Keys

Actionscript:
  1. var key:Object = new Object();
  2. stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
  3. stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
  4. function onKeyPressed(evt:KeyboardEvent):void {
  5.     key[evt.keyCode] = true;
  6.     key.keyCode = evt.keyCode;
  7. }
  8. function onKeyReleased(evt:KeyboardEvent):void {
  9.     key[evt.keyCode] = false
  10. }
  11.  
  12. // example
  13.  
  14. addEventListener(Event.ENTER_FRAME, onLoop);
  15. function onLoop(evt:Event):void {
  16.    
  17.     //trace(key.keyCode);
  18.      
  19.     if (key[Keyboard.LEFT]){
  20.         trace("left");
  21.     }
  22.    
  23.     if (key[Keyboard.RIGHT]){
  24.         trace("right");
  25.     }
  26.    
  27.     // keys #1, #2 and #3 are down
  28.     if (key[49] && key[50] && key[51]){
  29.         trace("one two thee");
  30.     }
  31.    
  32.     // keys #6, #7, #8 and #9 keys are down
  33.     if (key[54] && key[55] && key[56] && key[57]){
  34.         trace("six seven eight nine");
  35.     }
  36. }

The first 10 lines of code make up this snippet. This is an easy way to keep track of multiple key presses. For games, this is the only key technique I use ... wrapped up in a Singleton.

Also posted in keys | Tagged , | 4 Comments

Rect vs Ellipse

Actionscript:
  1. for (var i:int = 0; i<100; i++){
  2.     graphics.lineStyle(0,0);
  3.     graphics[["drawEllipse", "drawRect"][int(Math.random()*2)]](Math.random()*400, Math.random()*300, Math.random()*100,Math.random()*100)
  4. }
  5.  
  6. /*
  7. WARNING: This code was written for fun. Use at your own risk.
  8. */

And a more readable version:

Actionscript:
  1. var methodChoices:Array = ["drawEllipse", "drawRect"];
  2. var method:String;
  3. var xp:Number, yp:Number, w:Number, h:Number;
  4. for (var i:int = 0; i<100; i++){
  5.     method = methodChoices[int(Math.random()*methodChoices.length)];
  6.     graphics.lineStyle(0,0);
  7.     xp = Math.random()*400;
  8.     yp = Math.random()*300;
  9.     w = Math.random()*100;
  10.     h = Math.random()*100;
  11.     graphics[method](xp, yp, w, h)
  12. }
  13.  
  14. /*
  15. WARNING: This code was written for fun. Use at your own risk.
  16. */

Here I use yesterdays associative array function call technique to do something different. Not really all that useful, but interesting....

Also posted in Graphics, functions, random | Leave a comment