Friday, November 20, 2015

Use dynamic created class names

Here is a simple way to assign instantiate a dynamic created class name:

import flash.utils.getDefinitionByName;
var myFont = new ( getDefinitionByName(fontClass) as Class);

Friday, May 15, 2015

Easy to read numbers in PHP

This script adds a dot after every third digit to make it easier to read. For example it turns 82974893 into 82.974.893

function easyToRead($string){
= str_split($string);
= count($parts);
= [];
if(($i+1) % 3 == 0 && $i > 0 && ($i+1) < $partscount){
return implode("",$out);

Thursday, October 9, 2014

Working with SQLite and Adobe Air

First you will need to define a global variable to have access to your database, it’s query and queue

private var sql:Object = new Object();

Next you will need to establish a connection at the start of your App and create the database.

private function initConn():void {
= new SQLConnection();
sql.conn.addEventListener(SQLEvent.OPEN, openConnHandler);
sql.conn.addEventListener(SQLErrorEvent.ERROR, SQLErrorHandler);
var folder:File = File.applicationStorageDirectory;
var dbFile:File = folder.resolvePath("DBFile.db");
try {;
catch (error:SQLError) {
+ "\n\n" + error.details);

Connect success? Great! Create a table if it does not exist.

private function openConnHandler(event:SQLEvent):void {

Connection error? Throw an error.

private function SQLErrorHandler(event:SQLErrorEvent):void {
+ "\n\n" + event.error.details);

Now we need a query handler to perform database queries. This query handler will be able to put queries in a queue when there is a query currently pending.

private function doQuery(cmd, qry):void {
if (!sql.cmd) {
"doQuery(" + cmd + ", " + qry + ")->Executed");
= cmd;
= new SQLStatement();
= sql.conn;
= qry;
sql.stmt.addEventListener(SQLEvent.RESULT, SQLResult);
sql.stmt.addEventListener(SQLErrorEvent.ERROR, SQLErrorHandler);
else {
"doQuery(" + cmd + ", " + qry + ")->Queued");
if (!sql.queue) {
= new Array();
sql.queue.push([cmd, qry]);

Now we need to handle the result of the SQL query. This function will handle the results based on the command that was passed to the ‘'doQuery()” command and execute a new query if there is something in the query queue.

private function SQLResult(event:SQLEvent):void {
"SQLResult(" + sql.cmd + ")")
var result = sql.stmt.getResult();
var insertID = result.lastInsertRowID;
var i:int = 0;
switch(sql.cmd) {
case "createtables":
"tables have been created");
//start your application here
case "getuser":
var numResults:int = ? : 0;
for (i; i < numResults; i++){
var row:Object =[i];
//loop through results
= '';
if (sql.queue.length > 0) {
var q = sql.queue.shift();
0], q[1]);

Now to get some data simply do the following

doQuery("getuser","SELECT username FROM mytable WHERE id=23");

The result can be handled in the “SQLResult()” function.

If you test your Air App locally on a Windows PC, the database file is located in C:\Users\[username]\AppData\Roaming\[your app id]\Local Store\DBFile.db

To view and edit the contents of the file, use an SQL database editor like “DB Browser for SQLite”

You can download it here –>

Tuesday, August 26, 2014

Site preload progress bar

Here is a very easy way to make a progress bar (or animation) for preloading your site. This script relies on jQuery and checks the load progress of the site’s images.
var checkInterval = setInterval(checkLoaded, 500); $(window).load(init); function checkLoaded(){ var allImages = $('img'); var totalLoaded = 0; for(var i=0; i<allImages.length; i++){ if(allImages[i].complete){ totalLoaded++; } } var loadedFloat = totalLoaded/allImages.length; //use the loadedFloat number (between 0 and 1) for your progress bar / animation } function init(){ //clear the checkLoaded interval call once page is loaded clearInterval(checkInterval); //rest of your code here... }

Friday, April 4, 2014

String encryption in PHP and AS3

The following encryption is not 100% safe because the Actionscript code can still be read by most flash decompilers and thus one can find the private key used to encrypt the data. Even encrypting the swf file using tools like SWF Encrypt is not 100% safe! This code was written to make it harder for people to cheat in online games, but certainly not impossible due to Flash’s (lack of) security.
This function will encrypt and decrypt any string with a user provided key. They key can be any length (the longer the better obviously). The resulting encrypted string is scrambled whenever you create a new encrypted string with the same string and key as input so it is harder to crack. I have only used URL safe characters for encryption so the encrypted string is URL safe and can also be used as a URL variable.

The functions also uses sha256 to encrypt the key. This is more secure and you can use any key length you want. Since sha256 is not a native function in AS3 you will need a sha256 class. You can download it here.
PHP and AS3 functions
Click here to download.
Example usage for both PHP and Flash AS3
encrypt("Your secret is safe with me!","wookiefeet");

In this example I used the string “wookiefeet” as an encryption key, but you can use any key you like. The longer and more complex the key, the better. When decrypting you obviously will also have to give the same key.

Make sure you encrypt your swf file using an swf encryption tool like SWF Encrypt, otherwise one could find out what encryption key you are using.

Prevent tampering of $_POST or $_GET data in PHP
This function also sends a sha256 string of the unencrypted message as a verification. The decryption has a self-check built in to make sure the decrypted string is the same as the encrypted string by taking the sha256 that was sent along with the encrypted string, and comparing it with the computed sha256 of the decrypted string. If they are not the same then decrypt() will return false.

Thursday, February 13, 2014

Property not found (fix)

Do you sometimes have this ‘glitch’ in Flash where the code fails to access a clip on the stage after you have gone to that frame using gotoAndStop? You debug and check that it is actually on that frame and it is, but the clip just isn’t there (yet), thus your code fails to attach event listeners or get properties from clips that are not there (yet). Here is a fix:

First add the following after your class has initialized. This will trigger the renderListener function when the stage is rendered.

stage.addEventListener(Event.RENDER, renderListener);

Next add the renderListener. This will trigger after your stage items have been initialized and are available. For convenience I have added a frameLabel check and used a switch statement to initialize the proper code for every frame.

private function renderListener(event:Event):void {
var s = this.currentFrameLabel;
case "intro":
//your intro frame is loaded and ready
case "gameover":
//your gameover frame is loaded and ready

When you go to another frame, you would use something like gotoAndStop(‘gameover’). This however is not quite enough to trigger the Render event. Add the following code right after your gotoAndStop() call in order to trigger the Render event:

//add this to trigger the Render event

Wednesday, November 20, 2013

Convert Object to URLVariables

Here is how:

private function objectToURLVariables(parameters:Object):URLVariables{
var paramsToSend:URLVariables = new URLVariables();
for (var i:String in parameters){
if (i != null){
if (parameters[i] is Array){
= parameters[i];
= parameters[i].toString();
return paramsToSend;