Search Results for: static initializer

static BitmapData References

Actionscript:
  1. package{
  2.    
  3.     import flash.display.*;
  4.     import flash.events.*;
  5.     import flash.geom.*;
  6.    
  7.     public class QuickCheckBox extends Sprite{
  8.        
  9.         private var _checked:Boolean;
  10.         private var _bitmap:Bitmap;
  11.         private var _canvas:BitmapData;
  12.        
  13.         private static var checked:BitmapData;
  14.         private static var unchecked:BitmapData;
  15.         private static var bg:Shape;
  16.         private static var ex:Shape;
  17.         private static var pnt:Point;
  18.        
  19.         // static init for BitmapData and drawing
  20.         {
  21.             trace("only render your graphics once");
  22.             pnt = new Point(0,0);
  23.             checked = new BitmapData(10,10, true, 0x00000000);
  24.             unchecked = new BitmapData(10, 10, true, 0x00000000);
  25.             bg = new Shape();
  26.             with(bg.graphics) lineStyle(0,0x000000), beginFill(0xEFEFEF), drawRect(0,0,9,9);
  27.             unchecked.draw(bg);
  28.             ex = new Shape();
  29.             with(ex.graphics) {
  30.                 lineStyle(2,0x333333), moveTo(3, 3),
  31.                 lineTo(7, 7), moveTo(7, 3), lineTo(3, 7);
  32.             }
  33.             checked.draw(bg);
  34.             checked.draw(ex);
  35.         }
  36.        
  37.         public function QuickCheckBox(value:Boolean = false):void{
  38.             _checked = value;
  39.             _canvas =  new BitmapData(10,10, true, 0x00000000);
  40.             _bitmap = new Bitmap(_canvas);
  41.             addChild(_bitmap);
  42.             buttonMode = true;
  43.             render();
  44.             addEventListener(MouseEvent.CLICK, onClick);
  45.         }
  46.        
  47.         public function render():void{
  48.              if (_checked){
  49.                  _canvas.copyPixels(QuickCheckBox.checked, _canvas.rect, pnt);
  50.              }else{
  51.                  _canvas.copyPixels(unchecked, _canvas.rect, pnt);
  52.             }
  53.         }
  54.        
  55.         private function onClick(evt:Event):void{
  56.             _checked = !_checked;
  57.             render();
  58.             this.dispatchEvent(new Event(Event.CHANGE, true));
  59.         }
  60.        
  61.         public function get checked():Boolean{
  62.             return _checked;
  63.         }
  64.        
  65.         public function set checked(val:Boolean):void{
  66.             _checked = val;
  67.             render();
  68.         }
  69.     }
  70. }

This checkbox class uses a static initializer. Static initializers can come in handy to initialize some static variables for all class instances to make use of. In this case I'm using the static initializer to create checked and unchecked BitmapData objects - all instances of QuickCheckBox use these two static BitmapData objects rather than create their own unique ones.

Here is some client code if you want to test this class out:

Actionscript:
  1. for (var i:int = 0; i<100; i++){
  2.  var checkBox:QuickCheckBox = new QuickCheckBox(true);
  3.  checkBox.x =100 +  i % 10 * 12;
  4.  checkBox.y = 100 + int(i / 10) * 12;
  5.  // uncheck a few checkboxes
  6.  if (checkBox.x == checkBox.y){
  7.      checkBox.checked = false;
  8.  }
  9.  addChild( checkBox);
  10.  // checkBox dispatches a change event
  11.  checkBox.addEventListener(Event.CHANGE, onChange);
  12. }
  13. function onChange(evt:Event):void{
  14.     trace(evt.currentTarget.checked);
  15. }
  16. /*outputs
  17. only render your graphics once
  18. */

This draws 100 checkboxes, - you'll also notice in the output window that the trace statement from QuickCheckBox only runs once.

Here are 100 instances of QuickCheckBox:

Posted in BitmapData, OOP, UI | Tagged , | 4 Comments

Crackable String Encoding

Actionscript:
  1. var encode:Object = new Object();
  2. var decode:Object = new Object();
  3. var a:Array = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".split("");
  4.  
  5. // change this for different encoded results
  6. var offset:int = 10;
  7.  
  8. for (var i:int = 0; i<a.length; i++){
  9.     var index:int = (a.length - i - offset) % a.length;
  10.     encode[a[i]] = a[index];
  11.     decode[a[index]] = a[i];
  12. }
  13.  
  14. function encodeString(str:String):String{
  15.     return map(str, encode);
  16. }
  17.  
  18. function decodeString(str:String):String{
  19.     return map(str, decode);
  20. }
  21.  
  22. function map(str:String, smode:Object):String{
  23.     var n:String = "";
  24.     for (var i:int = 0; i<str.length; i++){
  25.         var char:String = str.charAt(i);
  26.         var en:String = smode[char];
  27.         if (en){
  28.             n += en;
  29.         }else{
  30.             n += char;
  31.         }
  32.     }
  33.     return n;
  34. }
  35.  
  36. // test out the functions
  37.  
  38. var input:String = "This is a regular string";
  39.  
  40. trace(input);
  41.  
  42. var encoded:String = encodeString(input);
  43.  
  44. trace("encoded: ", encoded);
  45.  
  46. trace("decoded: ",decodeString(encoded));
  47.  
  48. /*
  49. outputs:
  50.  
  51. This is a regular string
  52. encoded:  gSRH1RH1Z1IVTFOZI1HGIRMT
  53. decoded:  This is a regular string
  54.  
  55. */

The above demos an intentionally simple string encoding technique.

This is a technique I use if I need to encode strings but don't care if someone figures out what the string value actually is. For me, this is more common than needing hard/impossible to crack string encoding algorithms. A good example is an e-card... a url for an ecard could look like this:

www.birthdaycardthing.com/?name=joe&age=32

or it could look like this:

www.birthdaycardthing.com/?i=brx&x=5p

I wrapped this snippet up into a class and made a few minor tweaks. The class is called SimpleCipher it has two static methods and one static property...

Actionscript:
  1. import com.actionsnippet.utils.SimpleCipher;
  2. // same as offset above, but can be set at any time
  3. // new encode and decode Objects will be calculated
  4. SimpleCipher.offset = 1;
  5. var input:String = "SimpleCipher encoding and decoding";
  6.              
  7. trace("input: ", input);
  8.              
  9. var encoded:String = SimpleCipher.encode(input);
  10.              
  11. trace("encoded: ", encoded);
  12. trace("decoded: ", SimpleCipher.decode(encoded));
  13. /*
  14. outputs:
  15. input:  SimpleCipher encoding and decoding
  16. encoded:  RhlokdBhogdq0dmbnchmf0 mc0cdbnchmf
  17. decoded:  SimpleCipher encoding and decoding
  18. */

Download SimpleCipher Class
(this class uses a static initializer... see yesterdays post)

Posted in OOP, strings | Tagged , | Leave a comment

Static Constructor / Initializer

Actionscript:
  1. package{
  2.    
  3.     public class Lookup{
  4.        
  5.         private static var _random:Array;
  6.         // static initializer
  7.         {
  8.             trace("I am a static initializer for " + Lookup);
  9.             init();
  10.         }
  11.        
  12.         private static function init():void{
  13.              _random = new Array();
  14.              // generate some filler values
  15.              for (var i:int = 0; i<100; i++){
  16.                 _random.push(Math.random() * i);
  17.              }
  18.         }
  19.        
  20.         public static function get random():Array{
  21.             return _random.concat();
  22.         }
  23.     }
  24. }

...and to test out the above class:

Actionscript:
  1. package {
  2.     import flash.display.Sprite;
  3.  
  4.     public class Main extends Sprite{
  5.         public function Main(){
  6.            trace(Lookup.random[50]);
  7.         }
  8.     }
  9. }
  10. /*
  11. outputs:
  12. I am a static initializer for [class Lookup]
  13. 5.4258062043227255
  14. */

The above shows how to use static initializers. I've found this to be very handy... in this case we create an array of random values....

Tomorrow I'll actually post a real class where I make use of this... the Lookup class in this case is really just filler code....

Posted in OOP | Tagged , | 1 Comment