Actionscript:
-
package{
-
-
import flash.display.*;
-
import flash.events.*;
-
import flash.geom.*;
-
-
public class QuickCheckBox extends Sprite{
-
-
private var _checked:Boolean;
-
private var _bitmap:Bitmap;
-
private var _canvas:BitmapData;
-
-
private static var checked:BitmapData;
-
private static var unchecked:BitmapData;
-
private static var bg:Shape;
-
private static var ex:Shape;
-
private static var pnt:Point;
-
-
// static init for BitmapData and drawing
-
{
-
trace("only render your graphics once");
-
pnt = new Point(0,0);
-
checked = new BitmapData(10,10, true, 0x00000000);
-
unchecked = new BitmapData(10, 10, true, 0x00000000);
-
bg = new Shape();
-
with(bg.graphics) lineStyle(0,0x000000), beginFill(0xEFEFEF), drawRect(0,0,9,9);
-
unchecked.draw(bg);
-
ex = new Shape();
-
with(ex.graphics) {
-
lineStyle(2,0x333333), moveTo(3, 3),
-
lineTo(7, 7), moveTo(7, 3), lineTo(3, 7);
-
}
-
checked.draw(bg);
-
checked.draw(ex);
-
}
-
-
public function QuickCheckBox(value:Boolean = false):void{
-
_checked = value;
-
_canvas = new BitmapData(10,10, true, 0x00000000);
-
_bitmap = new Bitmap(_canvas);
-
addChild(_bitmap);
-
buttonMode = true;
-
render();
-
addEventListener(MouseEvent.CLICK, onClick);
-
}
-
-
public function render():void{
-
if (_checked){
-
_canvas.copyPixels(QuickCheckBox.checked, _canvas.rect, pnt);
-
}else{
-
_canvas.copyPixels(unchecked, _canvas.rect, pnt);
-
}
-
}
-
-
private function onClick(evt:Event):void{
-
_checked = !_checked;
-
render();
-
this.dispatchEvent(new Event(Event.CHANGE, true));
-
}
-
-
public function get checked():Boolean{
-
return _checked;
-
}
-
-
public function set checked(val:Boolean):void{
-
_checked = val;
-
render();
-
}
-
}
-
}
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:
-
for (var i:int = 0; i<100; i++){
-
var checkBox:QuickCheckBox = new QuickCheckBox(true);
-
checkBox.x =100 + i % 10 * 12;
-
checkBox.y = 100 + int(i / 10) * 12;
-
// uncheck a few checkboxes
-
if (checkBox.x == checkBox.y){
-
checkBox.checked = false;
-
}
-
addChild( checkBox);
-
// checkBox dispatches a change event
-
checkBox.addEventListener(Event.CHANGE, onChange);
-
}
-
function onChange(evt:Event):void{
-
trace(evt.currentTarget.checked);
-
}
-
/*outputs
-
only render your graphics once
-
*/
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:
By Zevan | February 8, 2009
Actionscript:
-
var encode:Object = new Object();
-
var decode:Object = new Object();
-
var a:Array = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".split("");
-
-
// change this for different encoded results
-
var offset:int = 10;
-
-
for (var i:int = 0; i<a.length; i++){
-
var index:int = (a.length - i - offset) % a.length;
-
encode[a[i]] = a[index];
-
decode[a[index]] = a[i];
-
}
-
-
function encodeString(str:String):String{
-
return map(str, encode);
-
}
-
-
function decodeString(str:String):String{
-
return map(str, decode);
-
}
-
-
function map(str:String, smode:Object):String{
-
var n:String = "";
-
for (var i:int = 0; i<str.length; i++){
-
var char:String = str.charAt(i);
-
var en:String = smode[char];
-
if (en){
-
n += en;
-
}else{
-
n += char;
-
}
-
}
-
return n;
-
}
-
-
// test out the functions
-
-
var input:String = "This is a regular string";
-
-
trace(input);
-
-
var encoded:String = encodeString(input);
-
-
trace("encoded: ", encoded);
-
-
trace("decoded: ",decodeString(encoded));
-
-
/*
-
outputs:
-
-
This is a regular string
-
encoded: gSRH1RH1Z1IVTFOZI1HGIRMT
-
decoded: This is a regular string
-
-
*/
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:
-
import com.actionsnippet.utils.SimpleCipher;
-
// same as offset above, but can be set at any time
-
// new encode and decode Objects will be calculated
-
SimpleCipher.offset = 1;
-
var input:String = "SimpleCipher encoding and decoding";
-
-
trace("input: ", input);
-
-
var encoded:String = SimpleCipher.encode(input);
-
-
trace("encoded: ", encoded);
-
trace("decoded: ", SimpleCipher.decode(encoded));
-
/*
-
outputs:
-
input: SimpleCipher encoding and decoding
-
encoded: RhlokdBhogdq0dmbnchmf0 mc0cdbnchmf
-
decoded: SimpleCipher encoding and decoding
-
*/
Download SimpleCipher Class
(this class uses a static initializer... see yesterdays post)
By Zevan | February 7, 2009
Actionscript:
-
package{
-
-
public class Lookup{
-
-
private static var _random:Array;
-
// static initializer
-
{
-
trace("I am a static initializer for " + Lookup);
-
init();
-
}
-
-
private static function init():void{
-
_random = new Array();
-
// generate some filler values
-
for (var i:int = 0; i<100; i++){
-
_random.push(Math.random() * i);
-
}
-
}
-
-
public static function get random():Array{
-
return _random.concat();
-
}
-
}
-
}
...and to test out the above class:
Actionscript:
-
package {
-
import flash.display.Sprite;
-
-
public class Main extends Sprite{
-
public function Main(){
-
trace(Lookup.random[50]);
-
}
-
}
-
}
-
/*
-
outputs:
-
I am a static initializer for [class Lookup]
-
5.4258062043227255
-
*/
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 actionscript, flash |