I just found some improvements on my ListenerProxy-Class and packed them into a new “Version 2″. I also added a license-information. You can get the new Version 2 of ListenerProxy here.
Updated List of References
I updated the List of References, because the link to the old version of the HugoBoss Flash-Shop was incorrect.
Check it out: List of Refrences
ListenerProxy::BUGFIX
I also removed duplicate entries of “if(objToAdd.hasOwnProperty(‘addEventListener’))”
Get the newest version[v1.3.4-stable]: ListenerProxy
Add methods to native Classes in ActionScript 3.0 – Prototype functions
Hi again!
Yesterday I’ve found out a cool new feature of ActionScript 3.0. You will sometimes have to use associative Arrays. The problem with these is that you can’t easily loop through them with a for-loop like
var myAssocArray:Array = [], i :int, intLength :int; myAssocArray['foo'] = 'one'; myAssocArray[1] = 'two'; myAssocArray['bar'] = 'three'; myAssocArray[0] = 'four'; intLength = myAssocArray.length; for(; i< intLength; ++i) { trace(myAssocArray[i]); }
The trace will probably print out “four, two, undefined, undefined”. The fact is that you can get the elements 0 & 1 via the variable i, but the the other keys can’t be accessed with this method.
As I also like PHP very much, I also know the function array_keys(array) of PHP, which returns the key-values of the array given in the parameters. I thought that I really need this functionality within AS3, so I tried a lot, searched the internet and finally got it! It’s pretty simple for those knowing AS2 – prototype functions.
What do we need to do? Place the following code in your main, unnamed package below or above the class-definition and you can use it everywhere else in the code:
package { public class foo { public function foo():void { trace('bar'); } } Array.prototype.arrayKeys = function():Array { var arrReturn :Array = [], strKey :String; for(strKey in this) { if(typeof this[strKey] == 'function') continue; trace('in Array:: value->'+ strKey +' typeOf:'+ typeof strKey +' proto:'+ ((this[strKey] !== false) ? typeof this[strKey] : 'NULL')); arrReturn[arrReturn.length] = strKey; } return arrReturn; } Number.prototype.give = function():String { return 'Number::giveItToMeBaby'; } }
You can now use this method to get the keys of the variable:
var arrKeys :Array = myAssocArray.arrayKeys(), i :int, intLength :int = arrKeys.length; for(; i < intLength; ++i) { trace(myAssocArray[arrKeys[i]]); }
Pretty easy, isn't it? Well, it is easy because the native Array-Class is dynamic, but what do we do with Number (final class)? Let's see...
var num:Number = 4; num.give(); //doesn't work! final class, can't add methods to it? num['give'](); //does work :) you can dynamically add methods to a class even if it's final; problem is that this code is hard to read, but it actually works
So far...
Daniel
if-statements – checking strings for being null
hello again!
Yesterday I wrote some code again and had to check if a String is null or not. The check was performed in a for-loop.
Below is some example code:
public var arrToFill:Array = []; [... some more code here ...] private function foo():void { var i:uint; for( i = 0; i < 10; ++i) { if(!arrToFill[i]) { arrToFill[i] = ''; } } }
This function was called different times and I wondered, that the if-statement returned false everytime.
For me it was theoretically correct that an Array-Element with a value of ” is not empty, because ” is an empty String for me, but it is something.
ActionScript3 doesn’t work like that, so for AS3 a String with a value of ” is equal to null or 0.
Conclusion:
If you have code like this and want to preallocate an element, just set it to 1. Then you can do a simpy
if(!arrToFill[i])
and everything would be fine.
[TIDY UP YOUR CODE] :: if – elseif – elseif – elseif – else
I thought about doing an extra section called ‘TIDY UP YOUR CODE’.
From time to time I will post some tips how you can write reader-friendly code.
So Let’ start with the first topic: if – elseif – elseif – elseif – else
Have you ever seen something like this ?
if(expressionA) { .. do something .. } elseif(expressionB) { .. do something else.. } elseif(expressionC) { .. do something else.. } elseif(expressionD) { .. do something else.. } else { .. do something else.. }
Code like this is hard to read, don’t you agree with this?
TIDY IT UP! Use a switch instead:
switch(true) { case expressionA: { .. do something .. }; break; case expressionB: { .. do something else .. }; break; case expressionC: { .. do something else .. }; break; case expressionD: { .. do something else .. }; break; default: { .. do something else .. }; break; }
In my opinion this is great :-}
Prevent using long if-expressions
Hello out there!
Today I learned how to check a lot of statements in one expression, but without having unreadable code like that:
if(this.strChar == 'a' || this.strChar == 'b' || this.strChar == 'c') { .. do something .. }
What you can do instead is creating an unnamed Array, push the values into it and then check if the Array contains one of those values.
Let’s see:
package { pubilc class foo { public function foo(strChar:String = 'default'):Boolean { if(['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'default'].indexOf(strChar) >= 0) { return true; } return false; } } }
i suppose that most of the code should be understandable to you, but let’s take a look at the if-statement:
if(['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'default'].indexOf(strChar) >= 0)
You could also use the following code with the same result:
var arrStringNumbers:Array = new Array(); arrStringNumbers.push('one', 'two', 'three' /*and so on... */); if(arrStringNumbers.indexOf(strChar) >= 0) // give me the index of strChar (if arrStringNumbers does not contain strChar, we get -1 as result, so the if-statement is false)
Welcome
Hello and welcome to my new website.
It’s the, ehm… let me think… third or perhaps fourth try of creating an own site.
Of course I’ve created some websites before but hadn’t have the time to do my very own site.
When I think deeper, the biggest problem was the content. What are people interested in? Should I post a simple Blog about my life, hobbies, car, etc.? No damn – nobody wants that crap!
I once had a site with some 3D-Graphics, but when I look at these today, I only think “What noob did this?”.
So after having a domain for about one and a half year, with nearly no content on it, I got the idea: ActionScript 3.0 would be the main topic of my website. Additional to this there may be a little bit PHP-Coding, C++(still learning the language at the moment) and perhaps some design-related content.
