Category Archives: arrays

Script List Pattern

Actionscript:
  1. var currentState:String = "";
  2.  
  3. var functionList:Vector.<Function> = new Vector.<Function>();
  4.  
  5. function clearFunctions():void{
  6.     functionList = new Vector.<Function>();
  7. }
  8. function addFunction(f:Function):Function {
  9.     functionList.push(f);
  10.     return addFunction;
  11. }
  12.  
  13. function removeFunction(f:Function):void {
  14.     for (var i:int = 0 ; i<functionList.length; i++){
  15.         if (f == functionList[i]){
  16.             functionList.splice(i, 1);
  17.         }
  18.     }
  19. }
  20.  
  21. function runProgram():void {
  22.    
  23.     currentState = "current: ";
  24.    
  25.     for (var i:int = 0; i<functionList.length; i++){
  26.         functionList[i]();
  27.     }
  28.    
  29.     trace(currentState);
  30. }
  31.  
  32. function one():void{
  33.     currentState += " one";
  34. }
  35.  
  36. function two():void {
  37.     currentState += " two";
  38. }
  39.  
  40. function three():void {
  41.     currentState += " three";
  42. }
  43.  
  44. function dot():void{
  45.     currentState += ".";
  46.    
  47. }
  48.  
  49. // test it:
  50. addFunction(one);
  51. addFunction(two);
  52. addFunction(three);
  53.  
  54. runProgram();
  55.  
  56. removeFunction(one);
  57.  
  58. runProgram();
  59.  
  60. addFunction(dot)(dot)(dot);
  61.  
  62. runProgram();
  63.  
  64. clearFunctions();
  65.  
  66. addFunction(dot)(dot)(dot);
  67.  
  68. addFunction(three)(two)(one)(dot)(dot)(dot);
  69.  
  70. runProgram();
  71.  
  72. /* outputs:
  73. current:  one two three
  74. current:  two three
  75. current:  two three...
  76. current: ... three two one...
  77. */

This is a very quick implementation of a pattern that I use sometimes. The idea of this pattern is very simple and can easily be implemented in OOP or procedural style programming. The idea is to have a Vector/Array of functions or Class instances. Loop through this Vector/Array and run each function (or a given method of each Class instance). During runtime your client code can alter this list to change what the program does.

I use this technique for games quite often. All enemies get added to an enemy list - this list is looped through and each enemies run() method is called. If an enemy dies it dispatches an event that tells the enemy manager to remove it from the list. Some pseudo code:

Actionscript:
  1. function onMainLoop():void{
  2.     if (!paused){
  3.        
  4.         runWorld();
  5.         runKeys();
  6.         runChar();
  7.        
  8.         enemyManager.runEnemies();
  9.        
  10.         runPickups();
  11.        
  12.     }else{
  13.         // show pause screen
  14.     }
  15. }
  16.  
  17. //... inside EnemyManager class
  18. function onRunEnemies():void{
  19.     for (var i:int = 0; i<enemyList.length; i++){
  20.             enemyList[i].run(i);
  21.     }
  22. }

I use the same technique for pickups (coins, lives etc....).

I first used this technique in Director with a list of parent scripts.

I'm aware of other more refined patterns that are meant to do similar things, but for small to medium sized apps this has worked very nicely for me.

Also posted in Vector, misc | Tagged , | 2 Comments

Dynamic Vars Dictionary

Actionscript:
  1. var vars:Dictionary = new Dictionary();
  2.  
  3. var sp:Sprite = new Sprite();
  4.  
  5. // associate variables with a sprite (or any non-dynamic class)
  6. vars[sp] = {posX:100, posY:100, velX:1, velY:1};
  7.  
  8. // read
  9. trace(vars[sp].posX);

I've heard people mention that they wish the sprite class were dynamic... meaning they wish they could add methods and properties to a Sprite instance at runtime. There's no way I know of to do this, however... the dictionary class can be used to associate variables with any non-dynamic class instance... as it does in this the above example.

The dictionary class is similar to an associative array except that instead of using strings for keys, dictionaries use object instances.

Also posted in Dictionary, dynamic | Tagged , | 1 Comment

2D Vector

Actionscript:
  1. var map:Vector.<Vector.<int>> = new Vector.<Vector.<int>>();
  2. map[0] = new Vector.<int>();
  3. map[0].push(1);
  4. map[0].push(0);
  5. map[0].push(0);
  6.  
  7. map[1] = new Vector.<int>();
  8. map[1].push(0);
  9. map[1].push(1);
  10. map[1].push(0);
  11.  
  12. map[2] = new Vector.<int>();
  13. map[2].push(0);
  14. map[2].push(0);
  15. map[2].push(1);
  16.  
  17. /*
  18. map:
  19. 1,0,0
  20. 0,1,0
  21. 0,0,1
  22. */

This creates a two dimensional Vector of ints (flash 10 player only). The first line is the real snippet... took me a few minutes to get the syntax right the first time I needed a 2D Vector.

Also posted in Vector | Tagged , | Leave a comment

Animate Along a Path

Actionscript:
  1. stage.frameRate = 30;
  2. var pencil:Shape = new Shape();
  3. addChild(pencil);
  4.  
  5. var index:int = 0;
  6. var points:Array = new Array();
  7. var circle:Shape = new Shape();
  8. circle.graphics.beginFill(0x000000);
  9. circle.graphics.drawCircle(0,0, 5);
  10. addChild(circle);
  11.  
  12. stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
  13. stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
  14.  
  15. circle.addEventListener(Event.ENTER_FRAME, onMoveCircle);
  16.  
  17. function onDown(evt:MouseEvent):void {
  18.     pencil.graphics.lineStyle(0,0x000000);
  19.     pencil.graphics.moveTo(mouseX, mouseY);
  20.     stage.addEventListener(MouseEvent.MOUSE_MOVE, onDraw);
  21. }
  22.  
  23. function onUp(evt:MouseEvent):void {
  24.     stage.removeEventListener(MouseEvent.MOUSE_MOVE, onDraw);
  25. }
  26.  
  27. function onDraw(evt:Event):void {
  28.     pencil.graphics.lineTo(mouseX, mouseY);
  29.     points.push(new Point(mouseX, mouseY));
  30. }
  31.  
  32. function onMoveCircle(evt:Event):void {
  33.     if (points.length>0) {
  34.         circle.x += (points[index].x - circle.x) / 4;
  35.         circle.y += (points[index].y - circle.y) / 4;
  36.         index++;
  37.         if (index==points.length) {
  38.             index=0;
  39.         }
  40.     }
  41. }

Wrote this in response to a student question. It causes a circle shape to animate along lines drawn by the user.

Also posted in motion | Tagged , | 2 Comments